nginx.conf配置文件location配置项与其中proxy_pass的后面加不加 /会对nginx最终的转向地址造成影响,这里列举会出现的情况以备忘。
1.配置文件(nginx.conf)
代码如下(示例):
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
error_page 405 =200 $request_uri;
}
#location后有 / proxy_pass后有 /
#最终nginx转向地址为http://192.168.251.1:80
location /prod-api/ {
proxy_pass http://192.168.251.1:80/;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#location后无 / proxy_pass后有 /
#最终nginx转向地址为http://192.168.251.1:80
location /prod-api {
proxy_pass http://192.168.251.1:80/;
}
#location后有 / proxy_pass后无 /
#最终nginx转向地址为http://192.168.251.1:80/prod-api
location /prod-api/ {
proxy_pass http://192.168.251.1:80;
}
#location后无 / proxy_pass后无 /
#最终nginx转向地址为http://192.168.251.1:80/prod-api
location /prod-api {
proxy_pass http://192.168.251.1:80;
}
}
总结
以上就是今天要讲的内容,本文介绍了nginx配置文件中location项的使用。