【问题标题】:How can I rewrite a request without changing the browers URL in nginx?如何在不更改 nginx 中的浏览器 URL 的情况下重写请求?
【发布时间】:2021-10-12 15:31:00
【问题描述】:

我正在使用nginx,而我的网络服务器htdocs 文件夹包含几个*.html 文件。即使请求不包含 *.html 扩展名,我也想为他们提供服务。

我在这里找到的大多数答案都应用了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;
}

【问题讨论】:

    标签: nginx nginx-config nginx-location


    【解决方案1】:

    您可以使用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;
    }
    

    【讨论】:

    • 我试过这个,没有成功。它仍然导致File not found
    • 我已经更新并提供了完整版,你可以试试
    猜你喜欢
    • 2014-01-23
    • 2015-11-15
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    相关资源
    最近更新 更多