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 了

posted @ 2020-03-22 11:51  Draymond  阅读(9417)  评论(0编辑  收藏  举报