nginx location 無效原因


http 請求80端口

https 請求443端口

 

server虛擬機1

  listen  10010;

  listen 443 ssl;

  server_name  域名1  域名2  域名3;

  監聽 域名1:10010  域名2:10010 域名3:10010 .........

 

server虛擬機2

  listen  80;

  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 了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM