首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在不更改nginx中的浏览器URL的情况下重写请求?

如何在不更改nginx中的浏览器URL的情况下重写请求?
EN

Stack Overflow用户
提问于 2021-10-12 01:51:27
回答 1查看 69关注 0票数 0

我使用的是nginx,我的webservers htdocs文件夹包含几个*.html文件。即使请求不包含*.html扩展,我也想为它们提供服务。

我在这里找到的大多数答案都适用于浏览器地址栏中可见的rewrite。我怎样才能做到同样但“默默”呢?

示例:www.example.com/foo => www.example.com/foo.html

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

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-12 02:11:03

你可以用try_files

代码语言:javascript
复制
location / {
    try_files $uri $uri/ $uri.html =404;
}

文件文档

代码语言:javascript
复制
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;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69534230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档