1. 地址转发
location /PToC/save/aaa {
proxy_pass https://127.0.0.1/xxxddd/api/v1/warning/out_create_warning;
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
}
2. root和alias
alias,访问前端资源/AAA/BBB/,指向资源地址:/home/user/test/
location /AAA/BBB/ { alias /home/user/test/; autoindex off; }
若访问的是,http://127.0.0.1/AAA/BBB/ccc,指向资源/home/user/test/ccc
使用说明:
(1)root
若访问/tmp/static/dist下,images目录下的一张图片,index.png
浏览器访问图片的路径为:http://192.168.1.1/images/index.png
如果是配置的root,则转为,/tmp/static/dist/images/index.png
nginx配置如下:
location /images {
root /tmp/static/dist;
}
root的处理方式:root的路径 + location的路径 + location匹配后面的路径(index.png)
(2)alias
若访问/tmp/static/dist下,images目录下的一张图片,index.png
浏览器访问图片的路径为:http://192.168.1.1/images/index.png
如果是配置的root,则转为,/tmp/static/dist/images/index.png
nginx配置如下(如果location 路径是以 / 结尾,则 alias 也必须是以 / 结尾):
location /images {
alias /tmp/static/dist/images;
}
alias的处理方式:alias的路径 + location匹配后面的路径(index.png)
3. 访问ip,本来是到一个页面,现在让重定向到另一个地址(记录待定)
$document_uri
是一个变量,它表示当前请求的 URI(不包括参数,即查询字符串),代表客户端请求的资源路径。
例如,如果客户端请求了 http://example.com/some/path?query=string
,那么 $document_uri
将会得到 /some/path
,而不包括 ?query=string
这部分。
location / { ####### cas start ####### set $flag 0; if ($document_uri = '/' ) { set $flag 1; } if ($document_uri = '/user/logout' ) { set $flag 1; } if ($flag = 1) { return 301 https://$server_addr/cas/login/#/login; } ####### cas end ######## include uwsgi_params; uwsgi_connect_timeout 3600; uwsgi_read_timeout 3600; uwsgi_send_timeout 3600; uwsgi_pass 127.0.0.1:9898; client_max_body_size 10240M; }
4. localtion转发 / 路由规则
proxy_pass 与 location带不带 / 的区别
浏览器发送 http://1.1.1.1/test/aaa 请求。
示例:
序号 | localtion | proxy_pass | 匹配规则 |
1 | /test | http://1.1.1.1 | /test/aaa |
2 | /test/ | http://1.1.1.1 | /test/aaa |
3 | /test | http://1.1.1.1/ | //aaa |
4 | /test/ | http://1.1.1.1/ | /aaa |