38

nginx之try_file、rewrite、proxy_pass的区别

 4 years ago
source link: https://www.wencst.com/archives/1955
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

rewrite会改变浏览器的URL链接,把原来的URL转发到新的URL上

try_file会转发URL到nginx代理的新的URL上,转发的同时,会以新的URL对应的location中的response为准,不会改变浏览器的URL

proxy_pass会转发URL到upstream对应的服务上,一般是外部服务,可以在upstream里解决端口问题和负载问题

rewrite用法举例:

rewrite /(.*) /abc/$1;

try_file用法举例:

try_files $uri /index.html;

proxy_pass用法举例:

upstream trace_server {
        ip_hash;
        #ip_hash表示根据ip固定转发请求。
        #fair (第三方)按后端服务器的响应时间来分配请求。响应时间短的优先分配。与weight分配策略相似。
        #默认为随机负载,每一个请求按时间顺序逐一分配到不同的后端服务器。
        server 10.0.0.11:9090 down; 
        server 10.0.0.11:8080 weight=2; 
        server 10.0.0.11:6060; 
        server 10.0.0.11:7070 backup; 
        #down 表示单前的server临时不參与负载.
        #weight 默觉得1.weight越大,负载的权重就越大。
        #max_fails :同意请求失败的次数默觉得1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
        #fail_timeout : max_fails次失败后。暂停的时间。
        #backup: 其他全部的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
    }
    location / {
        index index.html;
        proxy_pass http://trace_server/;
    }

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK