我有下一个 nginx 配置,url /网站/博客/index.php有效,但网址 /网站/博客/不起作用
server {
listen 80;
root /var/www/site;
index index.html index.htm index.php;
server_name site.com;
rewrite_log on
#IMPORTANT
location ~ /.*(\.py|\.ini)$ {
deny all;
}
location ^~ /website/blog/ {
rewrite ^.*$ /website/blog/index.php$request_filename last;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
答案1
根据您的评论,这实际上看起来可能是基本语法问题。将来,我强烈建议运行nginx -t
(without sudo
) 来显示配置错误。
我认为尝试文件比重写更可取,但无论哪种方式,以下都是我在 Moodle 网站上使用的方法。
location / {
try_files $uri $uri/ /index.php/$request_uri;
}
location ~ .php($|/) {
set $script $uri;
if ($uri ~ "^(.+.php)(/.+)") {
set $script $1;
}
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_temp_file_write_size 10m;
fastcgi_busy_buffers_size 512k;
fastcgi_buffer_size 512k;
fastcgi_buffers 16 512k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_intercept_errors on;
fastcgi_next_upstream error invalid_header timeout http_500;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
不幸的是,确切的重写规则似乎需要针对每个引擎进行细微的改变,但是 nginx 足够流行,以至于谷歌搜索nginx url <<engine, eg wordpress>>
会给你一些建议。