nginx配置

nginx配置

location /

结论
1.proxy_pass代理地址端口后有目录(包括 / ),转发后地址:代理地址+访问URL目录+去除location匹配目录
2.proxy_pass代理地址端口后无任何,转发后地址:代理地址+访问URL目录+携带location配置的目录

  • location与proxy_pass都配置/,则不携带location配置的path
# www.test.com/api/upload-->http://127.0.0.1:8080/upload
location /api/ {
    proxy_pass http://127.0.0.1:8080/;
}
  • location不带/,proxy_pass带/,则真实地址会带/
#  www.test.com/api/upload-->http://127.0.0.1:8080//upload
location /api {
    proxy_pass http://127.0.0.1:8080/;
}
  • location带/,proxy_pass不带/,则真实地址会带location匹配目录/api/
#  www.test.com/api/upload-->http://127.0.0.1:8080/api/upload
location /api/ {
    proxy_pass http://127.0.0.1:8080;
}
  • location和proxy_pass都不带/,则真实地址会带location匹配目录/api/
# www.test.com/api/upload-->http://127.0.0.1:8080/api/upload
location /api {
    proxy_pass http://127.0.0.1:8080;
}
  • 同1,但 proxy_pass带地址
# www.test.com/api/upload-->http://127.0.0.1:8080/server/upload
location /api/ {
    proxy_pass http://127.0.0.1:8080/server/;
}

root | alias

alias是把location的值替换掉,而root是拼接上location的值。
假设文件在我们服务器的路径/img/1.jpg。

  • 当配置alias时
# 访问http://server_name/pic/1.jpg则能正常查看图片。
location /pic/ {
  alias /img/
}
  • 当配置root时
# 访问http://server_name/pic/img/1.jpg则能正常查看图片
location /pic/ {
  root /img/
}

vue history 404

  • 项目配置
location /{
  root  /website/wenfu_zszk/;
  index index.html;

  #解决404
  try_files $uri $uri/ /index.html;
}

location /kp {
        alias  /usr/local/nginx/html/kp;
        index  index.html;
        try_files $uri $uri/ /kp/index.html;
}
  • 正则项目配置
server {
     listen 80;
     server_name a;
     root /data/wwwroot/a;
     index index.html;
     location ~ /(\w+)/(\w+)/(\w+) {     
         try_files $uri $uri/ /$1/$2/index.html;
         index /index.html; 
     } 
     access_log /data/logs/nginx/a_access.log; 
     error_log  /data/logs/nginx/a_error.log;
 }
posted @ 2022-05-04 21:45  翰林小院  阅读(69)  评论(0编辑  收藏  举报