在 Nginx 配置文件中,确实像下面这样设置了重写位置,但是不起作用并且每次都返回 404 未找到,这里是什么问题?
server {
listen 80;
server_name mydomain.com www.mydomain.com;
root /var/www/html/mydomain.com;
.
.
.
location /postfixadmin {
root /var/www/html/postfixadmin/;
index index.php index.html index.htm;
location ~ ^/postfixadmin/(.+\.php)$ {
try_files $uri $uri/ =404;
root /var/www/html/postfixadmin/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/postfixadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /var/www/html/postfixadmin/;
}
}
location /PostfixAdmin {
rewrite ^/* /postfixadmin last;
}
.
.
}
答案1
您的配置存在几个问题。
首先,您在 locations 内使用了root
指令。这很可能是导致 404 错误的原因。
您应该将第一个位置块更改为如下形式:
location /postfixadmin {
alias /var/www/html/postfixadmin/;
然后,您应该root
从第一个内部location
块中删除该指令。
第一次内部location
启动应该是这样的:
location ~ ^/postfixadmin/.+\.php$ {
try_files $uri =404;
您根本不需要第二个内部location
块,因为别名是从父location
块继承的。
最后,你应该将最后一个改为location
如下所示:
location /PostFixAdmin {
rewrite ^ /postfixadmin last;
}
答案2
通过更改根位置解决问题
正确的配置是:
server {
listen 80;
server_name mydomain.com www.mydomain.com;
root /var/www/html/mydomain.com;
.
.
.
location /postfixadmin {
root /var/www/html/;
index index.php index.html index.htm;
location ~ ^/postfixadmin/(.+\.php)$ {
try_files $uri $uri/ =404;
root /var/www/html/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
}
location ~* ^/postfixadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
root /var/www/html/;
}
}
location /PostfixAdmin {
rewrite ^/* /postfixadmin last;
}
.
.
}