每当我在url nginx中返回一个已知的文件扩展名404 Not Found.
domain.com/myroute.foo和domain.com/foo/myroute.foo是好的,但domain.com/myroute.php和domain.com/foo/myroute.php(或例如的CSS,.js文件)返回404 Not Found.
我的nginx服务器配置:
server {
listen 80;
server_name domain.com;
root /var/www/path/public;
charset utf-8;
gzip on;
#access_log logs/host.access.log main;
location / {
index index.html index.php index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
access_log off;
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不像任何其他网址那样/myroute.php将带有已知文件扩展名()的index.php网址转到我的文件中?
myroute.php 在您的服务器上不存在.
location按此顺序检查Nginx 指令
- 带有"="前缀且与查询完全匹配的指令(文字字符串).如果找到,搜索停止.
- 所有剩余的指令与传统的字符串.如果此匹配使用"^〜"前缀,则搜索停止.
- 正则表达式,按照在配置文件中定义的顺序.
- 如果#3产生匹配,则使用该结果.否则,使用#2的匹配
这意味着您的myroute.php请求由~ \.php$位置块处理,根据您的try_files指令产生404.
要解决这个问题,您需要使您的位置指令更具体(例如~ index\.php$),或者像在执行时一样使用try_files location /.使用重写也可以解决您的问题.
编辑:
重要的是要了解nginx选择位置块的顺序与其他位置块的顺序.在nginx维基上查看更多信息
关于你的问题,我认为最简单的解决方案是使用try_files
try_files $uri $uri/ /index.php?q=$uri&$args;
Run Code Online (Sandbox Code Playgroud)
在你location ~ \.php$ {和你的location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {块中
try_files $uri =404从.php$块中删除旧的您的最终配置文件现在应该如下所示
server {
listen 80;
server_name domain.com;
root /var/www/path/public;
charset utf-8;
gzip on;
#access_log logs/host.access.log main;
location / {
index index.html index.php index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js) {
try_files $uri $uri/ /index.php?q=$uri&$args;
add_header Cache-Control public;
add_header Cache-Control must-revalidate;
expires 7d;
access_log off;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3046 次 |
| 最近记录: |