我如何重写一个请求而不改变浏览器的URL在nginx?



我使用nginx和我的webservershtdocs文件夹包含几个*.html文件。我想为他们服务,即使请求不包含*.html扩展。

我在SO上找到的大多数答案都应用rewrite,这将在浏览器地址栏中可见。我怎样才能"悄无声息"地达到同样的效果呢?

的例子:www.example.com/foo =比;www.example.com/foo.html

如果我没弄错的话,/opt/bitnami/apps/wordpress/conf/nginx-app.conf是应用这些更改的最佳位置。

index index.php index.html index.htm;
if ($request_uri !~ "^/phpmyadmin.*$")
{
set $test  A;
}
if ($request_uri !~ "^/bitnami.*$")
{
set $test  "${test}B";
}
if (!-e $request_filename)
{
set $test  "${test}C";
}
if ($test = ABC) {
rewrite ^/(.+)$ /index.php?q=$1 last;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*.php$ {
deny all;
}
# Disable logging for not found files and access log for the favicon and robots
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";
# Deny all attempts to access hidden files such as .htaccess or .htpasswd.
location ~ /. {
deny all;
}

location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_param  SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}

您可以使用try_files

location / {
try_files $uri $uri/ $uri.html =404;
}

文档地址:https://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

index index.php index.html index.htm;
if ($request_uri !~ "^/phpmyadmin.*$")
{
set $test  A;
}
if ($request_uri !~ "^/bitnami.*$")
{
set $test  "${test}B";
}
if (!-e $request_filename)
{
set $test  "${test}C";
}
if (!-e $request_filename.html)
{
set $test  "${test}D";
}
if ($test = ABCD) {
rewrite ^/(.+)$ /index.php?q=$1 last;
}
location / {
try_files $uri $uri/ $uri.html =404;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*.php$ {
deny all;
}
# Disable logging for not found files and access log for the favicon and robots
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

include "/opt/bitnami/apps/bitnami/banner/conf/banner-substitutions.conf";
include "/opt/bitnami/apps/bitnami/banner/conf/banner.conf";
# Deny all attempts to access hidden files such as .htaccess or .htpasswd.
location ~ /. {
deny all;
}

location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_read_timeout 300;
fastcgi_pass unix:/opt/bitnami/php/var/run/www.sock;
fastcgi_index index.php;
fastcgi_param  SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}

相关内容

  • 没有找到相关文章

最新更新