http 請求80端口html
https 請求443端口nginx
server虛擬機1web
listen 10010;api
listen 443 ssl;服務器
server_name 域名1 域名2 域名3;websocket
監聽 域名1:10010 域名2:10010 域名3:10010 .........session
server虛擬機2socket
listen 80;spa
server_name 域名1 域名2 域名3;代理
監聽 域名1:80域名2:80域名3:80.........
server的 server_name:losten_port 決定惟一性
若是請求爲http:則被 server虛擬機2 代理,再依據uri 匹配 location
舉例:::
server { listen 443 ssl; server_name draymond7107.com; index index.html index.htm; ssl_certificate /usr/local/nginx/conf/cert/***.pem; ssl_certificate_key /usr/local/nginx/conf/cert/***.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; location /websocket{ allow all; index index.html index.htm; proxy_pass http://localhost:10010; include proxy.conf; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 「Upgrade」; } location / { index index.html index.htm; proxy_pass http://draymond_api; //負載 include proxy.conf; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
server { listen 80; server_name draymond.com draymond7107.com ; //監聽 域名爲 draymond.com draymond7107.com 端口爲 80 的請求(域名須要解析到本服務器的ip上(把www.baidu.com配到此處,請求百度首頁也不會被代理)) index index.html index.htm; location / { index index.html index.htm; proxy_pass http://draymond_api; include proxy.conf; } location /nginx-status { stub_status on; access_log on; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
同一個虛擬主機,配置的listen 不一樣,則監聽的端口也不一樣(爲何server_name能夠重複的緣由)
請求
http://draymond7107.com/websocket 被虛擬機2(80端口) 代理,致使將請求轉發到location / {}
https://draymond7107.com/websocket 被虛擬機1(443端口) 代理,請求轉發到 location /websocket{}
轉發路徑
虛擬主機:端口號 /location
當location不起做用的時候,排查下 請求的虛擬主機與端口號,,而後再排查具體轉發到哪一個 location 了