我希望我的nginx make显示所有url的干净.
通过一些研究,我已经将第一个案例付诸实践.它通过以下配置完成:
location / {
root html;
index index.html index.htm index.php;
try_files $uri.html $uri/ =404;
}
Run Code Online (Sandbox Code Playgroud)
它适用于indexhtml.html显示为indexhtml,但.php没有任何反应.如果我将$ uri.html更改为$ uri.php,它既不适用于.html,也不适用于.php.我试图在php位置添加类似的东西,但没有任何成功.
有什么建议吗?
Moh*_*ady 47
无需额外的块和命名位置以及所有内容.同时将index
线移到位置块之外
server {
index index.html index.php;
location / {
try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
}
location ~ \.php$ {
try_files $uri =404;
# add fastcgi_pass line here, depending if you use socket or port
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,如果你有一个文件夹,并在同一文件夹内具有相同名称的文件,像/folder/xyz/
和/folder/xyz.php
如果该文件夹,您将无法运行PHP文件xyz
包含一个index.php
或者index.html
,只要记住这一点.
Jac*_*ack 45
根据我的研究,如果你附加你的/etc/nginx/conf.d/domain.tld.conf文件包括:
location / {
try_files $uri $uri.html $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location ~ \.php$ {
try_files $uri =404;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}
Run Code Online (Sandbox Code Playgroud)
然后重新启动nginx并试一试.希望这会对你有所帮助!可以在这里找到更多信息(我在哪里找到)@ tweaktalk.net
为了进一步穆罕默德的答案,你可能还需要从提供重定向.html
和.php
对扩展名的版本.
这可以通过$request_uri
包含"完整原始请求URI(带参数)" 的事实来实现,并且不受用户不可见的内部重写的影响.
server {
index index.html index.php;
location / {
if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
try_files $uri $uri/ $uri.html $uri.php?$args;
}
location ~ \.php$ {
if ($request_uri ~ ^/([^?]*)\.php($|\?)) { return 302 /$1?$args; }
try_files $uri =404;
# add fastcgi_pass line here, depending if you use socket or port
}
}
Run Code Online (Sandbox Code Playgroud)
这对我来说已经有效了 5 年多了。
location / {
try_files $uri/ $uri.html $uri.php$is_args$query_string;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
53293 次 |
最近记录: |