Nginx

NGINX 在子文件夾中獲取應用程序以使用重定向

  • December 10, 2021

我已將Mautic安裝在子文件夾中,例如:example.com/m

大多數 Mautic 在我的配置(管理面板,使用 Mautic 等)中執行良好,但對於我的子文件夾中的一些重定向,我得到404

example.com/m 200
example.com/m/form/9 200
example.com/m/form/generate.js?id=9 404

(/form 不存在 - 它應該由 Mautic 動態重定向/生成)

文件系統上的所有文件都屬於 www-data。

我的主機 nginx conf:

server {
   server_name www.example.com;
   rewrite_log on;

   root /var/www/example.com/www.example.com;
   index index.php index.html index.htm;

   access_log /var/log/nginx/example.com_access.log;
   error_log /var/log/nginx/example.com_error.log notice;

   #######################################
   ##  Start Mautic Specific config #####
   #######################################

   location /m {
       # Working despite forms:
       try_files $uri $uri/ /m/?q=$uri&$args;
   }

   #######################################
   ##  End Mautic Specific config #####
   #######################################

   location / {
       try_files $uri $uri/ /index.php$is_args$args;
   }

   location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
   }

   # A long browser cache lifetime can speed up repeat visits to your page
   location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
      access_log        off;
      log_not_found     off;
      expires           360d;
   }

   # disable access to hidden files
   location ~ /\.ht {
     access_log off;
     log_not_found off;
     deny all;
   }

   listen [::]:443 ssl; # managed by Certbot
   listen 443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/stats.example.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/stats.example.com/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
   listen 443 ssl;
   server_name example.com;
   listen [::]:443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/stats.example.com/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/stats.example.com/privkey.pem; # managed by Certbot
   include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
   ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
   return 301 https://www.example.com$request_uri;

}

server {
   if ($host = example.com) {
       return 301 https://$host$request_uri;
   } # managed by Certbot


   if ($host = www.example.com) {
       return 301 https://$host$request_uri;
   } # managed by Certbot


   server_name example.com www.example.com;
   listen 80;
   listen [::]:80;
   return 301 https://$host$request_uri;
   #return 404; # managed by Certbot

}

我究竟做錯了什麼?

您希望以 開頭的 URI/mlocation /m塊處理。

但是,正則表達式位置優先,因此 URI/m/form/generate.js實際上由另一個位置處理,該位置與所有以 . 結尾的 URI 匹配.js。請參閱此文件

有一個^~運算符強制前綴位置優先,但這在您的情況下不起作用,因為應用程序是 PHP,並且您需要.php以塊處理的URI location ~ \.php$

因此,我建議您將location導致問題的原因嵌套在location /塊中。

例如:

location /m {
   try_files $uri $uri/ /m/?q=$uri&$args;
}

location / {
   try_files $uri $uri/ /index.php$is_args$args;

   location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
       access_log        off;
       log_not_found     off;
       expires           360d;
   }
}

location ~ \.php$ {
   ...
}

location ~ /\.ht {
   ...
}

引用自:https://serverfault.com/questions/1085888