nginx root alias proxypass

root

     如果一个请求的 URI 是/weblogs/test.html时,web 服务器将会返回服务器上的/data/weblogs/b.hundred.com/weblogs/test.html 的文件。

  

 

alias

       

  • uri为/mmp/mysite.html,请求后的页面是/data/weblogs/b.hundred.com/mysite.html.
  • /mmp/被替换为/data/weblogs/b.hundred.com/

   使用 alias 时,目录名后面一定要加”/”。

一般情况下,在location /中配置root,在location /other中配置alias。

 

proxy_pass  最后带上/会把匹配的部分吃掉,没有带上/会把匹配的东西也带上

下面四种情况分别用http://192.168.1.4/proxy/test.html 进行访问。
第一种:

  location /proxy/ {
     proxy_pass http://127.0.0.1:81/;
}

会被代理到http://127.0.0.1:81/test.html 这个url
 
第二咱(相对于第一种,最后少一个 /)

   location /proxy/ {
     proxy_pass http://127.0.0.1:81;
}

会被代理到http://127.0.0.1:81/proxy/test.html 这个url
 
第三种:

location /proxy/ {
     proxy_pass http://127.0.0.1:81/ftlynx/;
}

会被代理到http://127.0.0.1:81/ftlynx/test.html 这个url。
 
第四种情况(相对于第三种,最后少一个 / ):

location /proxy/ {
     proxy_pass http://127.0.0.1:81/ftlynx;
}

会被代理到http://127.0.0.1:81/ftlynxtest.html 这个url

 

第五种情况

location /oms_api {
proxy_pass http://omsapi/;

域名/oms_api/123===http://omsapi/123

 

第六种情况

location /api {
proxy_pass http://10.200.25.79:8038;

http://hexpress-ui.test.ydjia.cn/api/MainWeb/GetSUserBase  ===http://10.200.25.79:8038/api/MainWeb/GetSUserBase 

 

 

 

 

server {
    listen      80;
    server_name www.test.com;
 
    # 情形A
    # 访问 http://www.test.com/testa/aaaa
    # 后端的request_uri为: /testa/aaaa
    location ^~ /testa/ {
        proxy_pass http://127.0.0.1:8801;
    }
    
    # 情形B
    # 访问 http://www.test.com/testb/bbbb
    # 后端的request_uri为: /bbbb
    location ^~ /testb/ {
        proxy_pass http://127.0.0.1:8801/;
    }
 
    # 情形C
    # 下面这段location是正确的
    location ~ /testc {
        proxy_pass http://127.0.0.1:8801;
    }
 
    # 情形D
    # 下面这段location是错误的
    #
    # nginx -t 时,会报如下错误: 
    #
    # 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 /opt/app/nginx/conf/vhost/test.conf:17
    # 
    # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"
    location ~ /testd {
        proxy_pass http://127.0.0.1:8801/;   # 记住,location为正则表达式时,不能这样写!!!
    }
 
    # 情形E
    # 访问 http://www.test.com/ccc/bbbb
    # 后端的request_uri为: /aaa/ccc/bbbb
    location /ccc/ {
        proxy_pass http://127.0.0.1:8801/aaa$request_uri;
    }
 
    # 情形F
    # 访问 http://www.test.com/namea/ddd
    # 后端的request_uri为: /yongfu?namea=ddd
    location /namea/ {
        rewrite    /namea/([^/]+) /yongfu?namea=$1 break;
        proxy_pass http://127.0.0.1:8801;
    }
 
    # 情形G
    # 访问 http://www.test.com/nameb/eee
    # 后端的request_uri为: /yongfu?nameb=eee
    location /nameb/ {
        rewrite    /nameb/([^/]+) /yongfu?nameb=$1 break;
        proxy_pass http://127.0.0.1:8801/;
    }
 
    access_log /data/logs/www/www.test.com.log;
}
 
server {
    listen      8801;
    server_name www.test.com;
    
    root        /data/www/test;
    index       index.php index.html;
 
    rewrite ^(.*)$ /test.php?u=$1 last;
 
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
 
    access_log /data/logs/www/www.test.com.8801.log;
}
 
 

 

posted @ 2021-05-11 22:32  我才是酷酷的飞  阅读(313)  评论(1编辑  收藏  举报