nginx为什么在二级目录访问

很多时候感觉对了 但是就是404

二级动态代理 可能是多一个斜杠

//  错误代理 

 //  “/” 结尾在URL处,表示反向代理时不是 “https://www.xxxxxxx.com/proxyname/” ,
 //  而是 “https://www.xxxxxxx.com/” ,否则容易出现404的情况
location /proxyname/ {
      proxy_pass https://www.xxxxxxx.com;
}

 // 正确代理  “ ^~ ” 开头这个意思是强制匹配,原因是这样优先级高,不要被其他匹配规则覆盖;
location ^~ /proxyname/ {
      proxy_pass https://www.xxxxxxx.com/;
}

二级目录静态代理

//  默认则无法直接到二级目录 需要对应location
//  alias  顾名思义别名  可以随便取名即可
//  默认 /usr/share/nginx/html
location /static/ {
    alias /var/www/app/static/;
}

// 或者  root
//  表示根目录  不能随便需要 root + location 对应
location /static/ {
  root /var/www/app/;
}

支持二级目录到pathinfo

location ~ \.php {
             fastcgi_pass    127.0 . 0.1 : 9000 ;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME   / scripts$fastcgi_script_name;
             include        fastcgi_params;
             ###############################################################
             set  $path_info "";
             set  $real_script_name $fastcgi_script_name;
             if  ($fastcgi_script_name ~  "^(.+?\.php)(/.+)$" ) {
             set  $real_script_name $ 1 ;
             set  $path_info $ 2 ;
             }
             fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
             fastcgi_param SCRIPT_NAME $real_script_name;
             fastcgi_param PATH_INFO $path_info;
             ##############################################################
         }

自动匹配到二级域名

set  $sub_domain  "" ;
         if  ( $http_host  ~  "(.+).soul.com$" ) {
             set  $sub_domain  $1 ;
         }
         
         if  ( $http_host  =  "www.soul.com" ) {
             set  $sub_domain  "" ;
         }
 
         if  ( $sub_domain  !=  "" ) {
             rewrite /(.+) / $sub_domain / $1  break ;
         }

自动对应目录到多级目录

location /media/ {
    rewrite ^/media/(.*)$  /usr/share/nginx/html/$1 permanent;
}
posted @ 2024-03-26 14:37  vx_guanchaoguo0  阅读(37)  评论(0编辑  收藏  举报