首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nginx配置没有尾随斜杠/并重定向到尾斜杠/

问nginx配置没有尾随斜杠/并重定向到尾斜杠/
EN

Server Fault用户
提问于 2020-12-12 17:07:00
回答 2查看 2.2K关注 0票数 -1

Nginx Config文件如下所示:

http块内的

代码语言:javascript
复制
server {
        listen *:80 default_server;
        server_name "";
        # return 444;
        root /var/www/default_server;
        error_log /var/log/nginx/error.default_server.log debug;
        access_log /var/log/nginx/access.default_server.log custom_made_access_log_1;
        port_in_redirect on;
        server_name_in_redirect on;
        server_tokens on;
        index index.html index.htm default.html default.htm index.php;
        merge_slashes on;
        location = /basic_status {
                stub_status on;
                allow 192.168.80.1;
                deny all;
        }
        location = / {
        }
        location / {
                try_files $uri $uri/ =404;
                location ~* ^/phpmyadmin$ {
                        rewrite ^/(phpmyadmin)$ /$1/ last;
                }
                location ~* ^/(.+\.php)$ {
                        include fastcgi.conf;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        fastcgi_intercept_errors on;
                        fastcgi_pass unix:/run/php-fpm/www.sock;
                        fastcgi_index index.php;
                        include fastcgi_params;
                }
                location ~* ^/(.+\.(png|swf|pdf|jpg|jpeg|gif|css|png|js|ico|html|htm|xml|txt|zip|rar|fla|mov|mp4))$ {
                }
        }
        location /phpmyadmin/ {
                        alias /var/www/default_server/phpmyadmin/;
                        error_log /var/log/nginx/error.default_server.phpMyAdmin.log debug;
                        access_log /var/log/nginx/access.default_server.phpMyAdmin.log custom_made_access_log_1;
#                       try_files $uri $uri/ /phpmyadmin/index.php?$query_string;
                        location ~* ^/phpmyadmin/setup {

                        }
                        location ~* ^/phpmyadmin/(.+\.php)$ {
                                include fastcgi.conf;
                                fastcgi_split_path_info ^/phpmyadmin/(.+?\.php)(.*)$;
                                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                                fastcgi_intercept_errors on;
                                fastcgi_pass unix:/run/php-fpm/www.sock;
                                fastcgi_index index.php;
                                include fastcgi_params;
                        }
                        location ~* ^/phpmyadmin/(.+\.(png|swf|pdf|jpg|jpeg|gif|css|png|js|ico|html|htm|xml|txt|zip|rar|fla|mov|mp4))$ {
                                #avoid processing of calls to unexisting static files
                                try_files $uri =404;
                        }
        }
}

我有两个案件如下:

案例1:http://192.168.80.129/phpmyadmin/

案例2:http://192.168.80.129/phpmyadmin

病例1效果很好。地点

代码语言:javascript
复制
location /phpmyadmin/

按预期工作。

问题继电器在案例2,这是没有尾随斜杠。

正如您在上面的配置中看到的那样:

在服务器指令下,我将"root“配置为:

代码语言:javascript
复制
root /var/www/default_server;

而且,正如您在上面的配置中所看到的,我在位置/phpmyadmin/中配置了

代码语言:javascript
复制
location /phpmyadmin/

以下内容如下:

代码语言:javascript
复制
alias /var/www/default_server/phpmyadmin/;

此外,在块位置内~* ^/phpmyadmin/(.+.php)$

代码语言:javascript
复制
location ~* ^/phpmyadmin/(.+\.php)$

我配置了以下内容

代码语言:javascript
复制
fastcgi_split_path_info ^/phpmyadmin/(.+?\.php)(.*)$;

在调试日志中可以看到以下内容:

一旦浏览到http://192.168.80.129/phpmyadmin,就会在调试日志中看到以下内容

代码语言:javascript
复制
2020/12/12 15:40:10 [debug] 52078#0: *1 HTTP/1.1 404 Not Found

以及完整日志文件的部分:

代码语言:javascript
复制
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "phpmyadmin/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "basic_status"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin$"
2020/12/12 15:40:10 [debug] 52078#0: *1 using configuration "^/phpmyadmin$"
2020/12/12 15:40:10 [debug] 52078#0: *1 http cl:-1 max:1048576
2020/12/12 15:40:10 [debug] 52078#0: *1 rewrite phase: 3
2020/12/12 15:40:10 [debug] 52078#0: *1 http script regex: "^/(phpmyadmin)$"
2020/12/12 15:40:10 [notice] 52078#0: *1 "^/(phpmyadmin)$" matches "/phpmyadmin", client: 192.168.80.1, server: , request: "GET /phpmyadmin HTTP/1.1", host: "192.168.80.129"
2020/12/12 15:40:10 [debug] 52078#0: *1 http script copy: "/"
2020/12/12 15:40:10 [debug] 52078#0: *1 http script capture: "phpmyadmin"
2020/12/12 15:40:10 [debug] 52078#0: *1 http script copy: "/"
2020/12/12 15:40:10 [debug] 52078#0: *1 http script regex end
2020/12/12 15:40:10 [notice] 52078#0: *1 rewritten data: "/phpmyadmin/", args: "", client: 192.168.80.1, server: , request: "GET /phpmyadmin HTTP/1.1", host: "192.168.80.129"
2020/12/12 15:40:10 [debug] 52078#0: *1 post rewrite phase: 4
2020/12/12 15:40:10 [debug] 52078#0: *1 uri changes: 11
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "phpmyadmin/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin/setup"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin/(.+\.php)$"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin/(.+\.(png|swf|pdf|jpg|jpeg|gif|css|png|js|ico|html|htm|xml|txt|zip|rar|fla|mov|mp4))$"
2020/12/12 15:40:10 [debug] 52078#0: *1 using configuration "/phpmyadmin/"
2020/12/12 15:40:10 [debug] 52078#0: *1 rewrite phase: 1
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "phpmyadmin/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin/setup"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin/(.+\.php)$"
2020/12/12 15:40:10 [debug] 52078#0: *1 using configuration "^/phpmyadmin/(.+\.php)$"
2020/12/12 15:40:10 [debug] 52078#0: *1 event timer del: 18: 707626498
2020/12/12 15:40:10 [debug] 52078#0: *1 http process request line
2020/12/12 15:40:10 [debug] 52078#0: *1 http request line: "GET /themes/pmahomme/jquery/jquery-ui.css HTTP/1.1"
2020/12/12 15:40:10 [debug] 52078#0: *1 http uri: "/themes/pmahomme/jquery/jquery-ui.css"
2020/12/12 15:40:10 [debug] 52078#0: *1 http args: ""
2020/12/12 15:40:10 [debug] 52078#0: *1 http exten: "css"
2020/12/12 15:40:10 [debug] 52078#0: *1 posix_memalign: 00005626A1D05AF0:4096 @16
2020/12/12 15:40:10 [debug] 52078#0: *1 http process request header line
2020/12/12 15:40:10 [debug] 52078#0: *1 http header: "Host: 192.168.80.129"
2020/12/12 15:40:10 [debug] 52078#0: *1 http header: "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:83.0) Gecko/20100101 Firefox/83.0"
2020/12/12 15:40:10 [debug] 52078#0: *1 http header: "Accept: text/css,*/*;q=0.1"
2020/12/12 15:40:10 [debug] 52078#0: *1 http header: "Accept-Language: en-US,en;q=0.5"
2020/12/12 15:40:10 [debug] 52078#0: *1 http header: "Accept-Encoding: gzip, deflate"
2020/12/12 15:40:10 [debug] 52078#0: *1 http header: "Connection: keep-alive"
2020/12/12 15:40:10 [debug] 52078#0: *1 http header done
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 0
2020/12/12 15:40:10 [debug] 52078#0: *1 rewrite phase: 1
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: "phpmyadmin/"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/phpmyadmin$"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/(.+\.php)$"
2020/12/12 15:40:10 [debug] 52078#0: *1 test location: ~ "^/(.+\.(png|swf|pdf|jpg|jpeg|gif|css|png|js|ico|html|htm|xml|txt|zip|rar|fla|mov|mp4))$"
2020/12/12 15:40:10 [debug] 52078#0: *1 using configuration "^/(.+\.(png|swf|pdf|jpg|jpeg|gif|css|png|js|ico|html|htm|xml|txt|zip|rar|fla|mov|mp4))$"
2020/12/12 15:40:10 [debug] 52078#0: *1 http cl:-1 max:1048576
2020/12/12 15:40:10 [debug] 52078#0: *1 rewrite phase: 3
2020/12/12 15:40:10 [debug] 52078#0: *1 post rewrite phase: 4
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 5
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 6
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 7
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 8
2020/12/12 15:40:10 [debug] 52078#0: *1 access phase: 9
2020/12/12 15:40:10 [debug] 52078#0: *1 access phase: 10
2020/12/12 15:40:10 [debug] 52078#0: *1 access phase: 11
2020/12/12 15:40:10 [debug] 52078#0: *1 post access phase: 12
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 13
2020/12/12 15:40:10 [debug] 52078#0: *1 generic phase: 14
2020/12/12 15:40:10 [debug] 52078#0: *1 content phase: 15
2020/12/12 15:40:10 [debug] 52078#0: *1 content phase: 16
2020/12/12 15:40:10 [debug] 52078#0: *1 content phase: 17
2020/12/12 15:40:10 [debug] 52078#0: *1 content phase: 18
2020/12/12 15:40:10 [debug] 52078#0: *1 content phase: 19
2020/12/12 15:40:10 [debug] 52078#0: *1 content phase: 20
2020/12/12 15:40:10 [debug] 52078#0: *1 http filename: "/var/www/default_server/themes/pmahomme/jquery/jquery-ui.css"
2020/12/12 15:40:10 [debug] 52078#0: *1 add cleanup: 00005626A1DA7AD8
2020/12/12 15:40:10 [error] 52078#0: *1 open() "/var/www/default_server/themes/pmahomme/jquery/jquery-ui.css" failed (2: No such file or directory), client: 192.168.80.1, server: , request: "GET /themes/pmahomme/jquery/jquery-ui.css HTTP/1.1", host: "192.168.80.129"
2020/12/12 15:40:10 [debug] 52078#0: *1 http finalize request: 404, "/themes/pmahomme/jquery/jquery-ui.css?" a:1, c:1
2020/12/12 15:40:10 [debug] 52078#0: *1 http special response: 404, "/themes/pmahomme/jquery/jquery-ui.css?"
2020/12/12 15:40:10 [debug] 52078#0: *1 http set discard body
2020/12/12 15:40:10 [debug] 52078#0: *1 xslt filter header
2020/12/12 15:40:10 [debug] 52078#0: *1 HTTP/1.1 404 Not Found
Server: nginx/1.18.0
Date: Sat, 12 Dec 2020 13:40:10 GMT
Content-Type: text/html
Content-Length: 153
Connection: keep-alive

我可以看到问题的根源从下一行开始。

代码语言:javascript
复制
2020/12/12 15:40:10 [debug] 52078#0: *1 http request line: "GET /themes/pmahomme/jquery/jquery-ui.css HTTP/1.1"
2020/12/12 15:40:10 [debug] 52078#0: *1 http uri: "/themes/pmahomme/jquery/jquery-ui.css"

正如您所看到的,问题是:

代码语言:javascript
复制
"GET /themes/pmahomme/jquery/jquery-ui.css HTTP/1.1"

为什么请求变成

代码语言:javascript
复制
/themes/pmahomme/jquery/jquery-ui.css

而不是

代码语言:javascript
复制
/phpmyadmin/themes/pmahomme/jquery/jquery-ui.css

因此,假设这是我想要实现的方式。我应该怎么做才能克服这个问题。

当位置块

代码语言:javascript
复制
location / {
        try_files $uri $uri/ =404;
        location ~* ^/phpmyadmin$ {
                rewrite ^/(phpmyadmin)$ /$1/ last;
        }
}

处理请求,然后重写工作正常,但我发现文件系统试图使用

代码语言:javascript
复制
root /var/www/default_server;

而不是使用别名配置。

代码语言:javascript
复制
alias /var/www/default_server/phpmyadmin/;

的位置块中。

代码语言:javascript
复制
location /phpmyadmin/
EN

回答 2

Server Fault用户

回答已采纳

发布于 2021-02-01 20:00:31

在最后,我所做的是以下方法:

代码语言:javascript
复制
location ~* ^/phpmyadmin$ {
return 301 http://$host:$server_port/phpmyadmin/;
}
    
location ~* ^/phpmyadmin/$ {
return 301 http://$host:$server_port/phpmyadmin/;
}

这样,我就能够将大写和小写重定向到特定的URL

票数 0
EN

Server Fault用户

发布于 2020-12-12 23:51:04

如果我对您的理解是正确的,那么您只想在/phpmyadmin/下使用D0。为什么你不直接把一个从/phpmyadmin到/phpmyadmin/的重定向(而不是在/phpmyadmin下交付内容)并完成它呢?

代码语言:javascript
复制
location /phpmyadmin
{
    return 303 https://example.com/phpmyadmin/;
}

未经测试。

票数 1
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1045947

复制
相关文章

相似问题

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