nginx 反向代理注意事项

如下配置: location /fileStorage {     proxy_pass http://Storage/;     proxy_set_header X-real-ip $remote_addr;     proxy_set_header Host $http_host;     } upstream  Stor...

如下配置:


location /fileStorage {

    proxy_pass http://Storage;

    proxy_set_header X-real-ip $remote_addr;

    proxy_set_header Host $http_host;

    }


upstream  Storage {
        ip_hash;
        server   192.168.1.56:8000;
        server   192.168.1.183:8000;
}


server{

       listen            8000;

       server_name       127.0.0.1 192.168.1.183;

       location ~* /\.(html|htm|jsp|php|js)$ {

          deny  all;

       }

       location /group1/M00{

           root /data/fastdfs;

           ngx_fastdfs_module;

       }

       location /{

            root    /www/nginx/html;

            expires  3d;

       }

    }



通过 strace  -s 2048 -f $(pidof nginx| sed 's/\([0-9]*\)/\-p \1/g') -o add.txt         可以看到,


访问的url路径为:/fileStorage/group1/M00/00/16/wKgBOF4B8WiAS0unAAGFXn8vebw921.ico 而被代理服务器上  location /group1/M00        ,并没有fileStorage ,所以匹配不上,

直接定位到了  root    /www/nginx/html; 这个目录下肯定没有 16/wKgBOF4B8WiAS0unAAGFXn8vebw921.ico  文件,故报404


如果请求路径中想去掉 /fileStorage 需要在   proxy_pass  http://Storage; 加上/ 变成:


     proxy_pass  http://Storage/;  即可。


还有一种情况,对于 像如下的正则匹配,则 proxy_pass http://Storage/;  会报错,
nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /webdata/conf/dqn_im_443.conf:111

此时只能通过rewrite重写来实现了。

location ~ ^/fileStorage/.*/.*\.(jpg|png|jpeg|gif)$
                {
                  rewrite ^/fileStorage/(.*)$ /$1 break; ##去掉fileStorage
                      proxy_pass http://Storage;
                      proxy_set_header X-real-ip $remote_addr;
                      proxy_set_header Host $http_host;
                      expires    7d;
                      access_log off;
                  }



反向代理:
proxy_pass 后跟的服务器URL值是否以 / 结尾的区别:

若 Nginx 收到请求为 https://ngxin_server_name/hello/world
而 Nginx 代理的路径为 /hello/(即在 location /hello/ 内设置代理)
则不以 / 结尾的被代理服务器收到的请求路径是 /hello/world
以 / 结尾的被代理服务器收到的请求路径是 /world
如果是为了在同一个域名下以不同路径分配不同的APP应选择后者以 / 结尾

Nginx 的代理HTTP版本默认为1.0,若要设置为1.1则需添加

proxy_http_version 1.1;
proxy_set_header Connection "";

URL 路径匹配:
这里没怎么踩坑,因为 Nginx 默认如果是普通路径匹配(无正则)优先匹配路径长的并不会继续往下匹配。(当然可以设置往下匹配)

若同时设置了 /app/hello 和 /app 两个路径,当请求URL应匹配到 /app 时,
会优先匹配更长的 /app/hello 并且不会继续往下匹配 /app。

另一个需要注意的地方是路径是否以 / 结尾,若以 / 结尾则路径可被目标服务器继承,反之则不可。

若设置的是 location /app {xxx} 而请求的路径为 /app/hello/world
尽管目标服务器的app设置了匹配 /hello/world 但只能匹配到 /hello
若设置的是 location /app/ {xxx} 而请求路劲为 /app/hello/world
目标服务器的app可以匹配到/*/*/*/...所有的路径 (/ 也看作是以 / 结尾)



使用 root和alias 区别

root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上。
root的处理结果是:root路径+location路径
alias的处理结果是:使用alias路径替换location路径
alias是一个目录别名的定义,root则是最上层目录的定义。
还有一个重要的区别是alias后面必须要用“/”结束,否则会找不到文件的。。。而root则可有可无~~

root实例:

1
2
3
location^~/t/{
     root/www/root/html/;
}

如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。

alias实例:

1
2
3
location^~/t/{
alias/www/root/html/new_t/;
}

如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/new_t/a.html的文件。注意这里是new_t,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。

注意:
1. 使用alias时,目录名后面一定要加"/"。
3. alias在使用正则匹配时,必须捕捉要匹配的内容并在指定的内容处使用。
4. alias只能位于location块中。(root可以不放在location中)


  • 发表于 2019-12-24 20:39
  • 阅读 ( 60 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
shitian
shitian

662 篇文章

作家榜 »

  1. shitian 662 文章
  2. 石天 437 文章
  3. 每天惠23 33 文章
  4. 小A 29 文章