Nginx轉發多個二級域名及前後端分離
一、直接上程式碼(以下配置方案為親在廣州某物聯網公司做架構時的配置)
/etc/nginx/conf.d/ssl.conf 配置檔案:
其中sink-node1與web-node1為內部伺服器的hostname。
#
# HTTPS server configuration
server {
listen 443;
server_name localhost;
ssl on;
root html;
index index.html index.htm;
ssl_certificate cert/all-yourdomain.pem;
ssl_certificate_key cert/all-yourdomain.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
### Required configuration. ###
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#
### Nginx supports websocket must be configured. ###
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
#
### gzip Compressed transmission. ###
gzip on;
gzip_min_length 1k; # min is 1kib
gzip_buffers 16 64K;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
gzip_vary on;
#
### Enhanced optional configuration. ###
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 60s;
proxy_send_timeout 120s;
proxy_read_timeout 86400s;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
#add_header Pragma “no-cache”;
#add_header Cache-Control “no-store, no-cache, must-revalidate, post-check=0, pre-check=0”;
location / {
if ($host = `www.yourdomain.com` ) {
proxy_pass http://web-node1:8080; break;
}
if ($host = `yourdomain.com` ) {
proxy_pass http://web-node1:8080; break;
}
if ($host = `passport.yourdomain.com` ) {
proxy_pass http://web-node1:8080; break;
}
if ($host = `www.passport.yourdomain.com` ) {
proxy_pass http://web-node1:8080; break;
}
if ($host = `mp.yourdomain.com` ) {
proxy_pass http://web-node1:8091; break;
}
if ($host = `www.mp.yourdomain.com` ) {
proxy_pass http://web-node1:8091; break;
}
if ($host = `m.yourdomain.com` ) {
proxy_pass http://web-node1:8091; break;
}
if ($host = `www.m.yourdomain.com` ) {
proxy_pass http://web-node1:8091; break;
}
if ($host = `image.yourdomain.com` ) {
proxy_pass http://web-node1:8083; break;
}
if ($host = `www.image.yourdomain.com` ) {
proxy_pass http://web-node1:8083; break;
}
if ($host = `wx.yourdomain.com` ) {
proxy_pass http://web-node1:9090; break;
}
if ($host = `www.wx.yourdomain.com` ) {
proxy_pass http://web-node1:9090; break;
}
if ($host = `im.yourdomain.com` ) {
proxy_pass http://sink-node1:10031; break;
}
if ($host = `www.im.yourdomain.com` ) {
proxy_pass http://sink-node1:10031; break;
}
if ($host = `ms.yourdomain.com` ) {
proxy_pass http://sink-node1:29088; break;
}
if ($host = `www.ms.yourdomain.com` ) {
proxy_pass http://sink-node1:29088; break;
}
if ($host = `kibana.yourdomain.com` ) {
proxy_pass http://sink-node1:5601; break;
}
if ($host = `www.kibana.yourdomain.com` ) {
proxy_pass http://sink-node1:5601; break;
}
# subproject為其中一個子系統(前後端分離)的二級域名轉發配置
# 以下邏輯為Nginx支援“與”、“或”、“非”等運算子的實現,See: https://www.jianshu.com/p/15a5cf8369f9
set $eff_flag `EFF`;
if ($host = `subproject.yourdomain.com` ) {
set $eff_flag `${eff_flag}F`;
}
if ($host = `www.subproject.yourdomain.com` ) {
set $eff_flag `${eff_flag}F`;
}
if ($request_uri ~* `/eff/*` ) {
set $eff_flag `${eff_flag}BKE`;
}
if ($eff_flag ~* `EFFFBKE` ) {
proxy_pass http://web-node1:29099; break;
}
# 關鍵此,這專門使用nginx來處理前端分離的靜態資源,而不是轉發到nodejs/webpack服務
# subproject static resource forwarding.
if ($eff_flag ~* `EFFF` ) {
proxy_pass http://web-node1:58888; break;
}
# Other pan parsing by cid.
proxy_pass http://web-node1:8082;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
/etc/nginx/conf.d/static.conf 配置檔案:
其中58888埠專為 子系統subproject開放的靜態資源埠,其實就是代替nodejs的web服務。
#
# HTTP front/static server configuration
server {
# The port that is transferred to the front end and the end is not open to the external network.
listen 58888;
server_name localhost;
location / {
# Fixed format (if additional front and back end separation items should be configured in this format),
# both root and index instructions are necessary, because the static file of the project will be under
# the directory of the root instruction, otherwise it will not be loaded..
root /usr/share/nginx/html/efficiency-front;
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
相關文章
- Nginx 同域名部署前後端分離專案Nginx後端
- Nginx代理同域名前後端分離專案Nginx後端
- cookie跨域共享 cookie二級域名共享 前後端分離專案共享cookieCookie跨域後端
- 前後端分離開發部署模式【轉】後端模式
- 前後端分離後模組開發後端
- docker+nginx+redis部署前後端分離專案!!!DockerNginxRedis後端
- centos7.9 配置nginx實現前後端分離CentOSNginx後端
- Nginx流量同時轉發多後端(流量映象分發)Nginx後端
- 前後端分離開發腳手架後端
- Nginx部署前後端分離服務以及配置說明Nginx後端
- 再談前後端分離後端
- 前後端分離那些事後端
- 淺談前後端分離後端
- 前後端分離——使用OSS後端
- 談談前後端分離及認證選擇後端
- 關於前後端分離及初始化配置後端
- 前後端分離後的前端時代後端前端
- docker-compose + nginx部署前後端分離的專案DockerNginx後端
- 通過nginx部署前端程式碼實現前後端分離Nginx前端後端
- 前後端不分離到分離演變,優勢,前後端介面聯調,排錯及優化後端優化
- ???前後端分離模式的思考???後端模式
- 前後端分離——資料mock後端Mock
- vue前後端分離修改webpackVue後端Web
- 前後端分離整合SpringSecurity後端SpringGse
- 前後端分離,最佳實踐後端
- 淺談WEB前後端分離Web後端
- 什麼是前後端分離?後端
- 前後端分離實踐有感後端
- 從MVC到前後端分離MVC後端
- 前後端分離Ajax入門後端
- vivo 商城前端架構升級—前後端分離篇前端架構後端
- nginx+tomcat單個域名及多個域名配置NginxTomcat
- SpringBoot+Vue前後端分離及互動Spring BootVue後端
- 前後端分離探索——MVC 專案升級的一個過渡方案後端MVC
- nginx配置二級域名Nginx
- 二級域名nginx 配置Nginx
- Django+Vue.js搭建前後端分離專案 web前後端分離專案實踐DjangoVue.js後端Web
- 使用Vue,Spring Boot,Flask,Django 完成Vue前後端分離開發(二)VueSpring BootFlaskDjango後端