在工作中,我们正在从共享LAMP堆栈切换到运行nginx的VPS。我对Apache非常熟悉,但学习使用Nginx非常令人兴奋(或者像学习配置新的网络服务器一样令人兴奋。
当前的问题是:在几个域上我们需要从规范化的URL中删除index.php。这是我们目前需要处理这些域的最后一件事。
我一直在研究从URL中删除index.php的大量不同技术(其中大部分专门用于CI或ExpressionEngine),我尝试将其中的几种用于个人用途,但我最终得到了一个我无法想象的无限循环错误与以下内容有关:
location / {
try_files $uri $uri/ /index.php?$args =404;
}
我很想知道所有这些是如何运作的,但是现在我需要寻求帮助来解决这个问题,这样我们才能继续前进,我可以弄清楚我做错了什么。
我会非常感谢任何回复,并会进一步感谢任何愿意深入研究这个问题的人,以帮助像我这样的新人以及可能处于类似情况的其他人。
谢谢!
更新
为了让事情变得简单,我只是将我的nginx配置放在这个虚拟主机上。
server {
listen 80;
server_name examplesite.com;
# redirect non-www to www. for canonical urls
rewrite ^/(.*) http://www.examplesite.com/$1 permanent;
}
server {
listen 80;
server_name www.examplesite.com;
error_log /srv/http/nginx/examplesite.com/log/nginx-error.log;
access_log /srv/http/nginx/examplesite.com/log/nginx-access.log;
root /srv/http/nginx/examplesite.com/root;
location / {
try_files $uri $uri/ /index.php?$args =404;
}
location ~ \.php$ {
include fastcgi.conf;
fastcgi_param PHP_ADMIN_VALUE "error_log=/srv/http/nginx/examplesite.com/log/php-error.log";
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
}
答案 0 :(得分:0)
首先,检查索引指令
server {
index index.php index.html;
...
然后确保php正由您配置的后端正确处理
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass backend;
}
后端是早期配置的上游,例如:
upstream backend {
server 127.0.0.1:9000;
}
答案 1 :(得分:0)
试试这个......
server {
listen 80;
server_name examplesite.com;
# redirect non-www to www. for canonical urls
# proper way to do this
rewrite ^ http://www.examplesite.com$request_uri? permanent;
}
server {
listen 80;
server_name www.examplesite.com;
error_log /srv/http/nginx/examplesite.com/log/nginx-error.log;
access_log /srv/http/nginx/examplesite.com/log/nginx-access.log;
root /srv/http/nginx/examplesite.com/root;
# add index under server as you have done for root
index index.php index.html;
location / {
# drop the "=404". Nginx will return 404 by itself if not found.
# note the "last" should not be added to "try_files"
# whether you need something after "$uri/" depends on your setup.
# this should do for most unless running WP etc
try_files $uri $uri/;
}
location ~ \.php$ {
# Return '400 Bad Request' for malformed URLs
# See: http://wiki.nginx.org/Pitfalls#Pass_Non-PHP_Requests_to_PHP.
location ~ \..*/.*\.php$ {
return 400;
}
# Rewrite "index.php" requests
rewrite ^(.*)index.php(.*)$ $1$2 permanent;
# continue for other php and rewritten "index.php" requests
include fastcgi.conf;
fastcgi_param PHP_ADMIN_VALUE "error_log=/srv/http/nginx/examplesite.com/log/php-error.log";
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}
...
}
关于index.php重写行不是100%,因为它可能会让你进入重定向循环。如果是这种情况,那么答案就是只提供index.php请求,但更改应用程序中的所有链接以删除它们,以便它们随着时间消失。