使用 Nginx 从 URL 中删除索引文件
在工作中,我们从共享 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;
}
}
At work we are switching from a shared LAMP stack to a VPS running nginx. I'm significantly more comfortable with Apache but learning to use Nginx has been pretty exciting (or as exciting as learning to configure a new webserver can be.
The current issue is this: on several domains we need to remove index.php from the URL for canonicalization. This is the last thing that we currently need to take care of for these domains.
I've been researching plenty of different techniques for removing index.php from a URL (most of which are specifically for CI or ExpressionEngine) and I've tried to adapt several of these for personal use but I end up with an infinite loop error which I can only imagine is related to the following:
location / {
try_files $uri $uri/ /index.php?$args =404;
}
I'm hell-bent on learning how all of this works but right now I need to ask for help figuring this out so that we can move forward and I can figure out what it is that I'm doing wrong.
I would greatly appreciate any responses and will further appreciate anyone who is willing to go a little in-depth on the subject to help someone new like myself and anyone else who might be in a similar situation that reads this.
Thank you!
UPDATE
To make things easier I'm just going to put my nginx config up here for this vhost.
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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个...
不是 100% 关于 index.php 重写行,因为它有可能让你陷入重定向循环。如果是这种情况,那么答案就是只提供 index.php 请求,但更改应用程序中的所有链接以删除它们,以便它们随着时间的推移而消失。
Try this ...
Not 100% about the index.php rewrite line as there is a chance it might put you in a redirect loop. If that is the case, then the answer is to just serve the index.php requests but change all links in the application to remove them so that they disappear over time.
首先,检查您的索引指令
然后确保您配置的后端正确处理 php
其中后端是之前配置的上游,例如:
查看 虚拟主机示例
At first, Check your index directive
Then be sure that php is properly handling by your configured backend
Where backend is the configured earlier upstream, for ex:
Look at the VirtualHost example