【问题标题】:Remove leading slash NGINX删除前导斜杠 NGINX
【发布时间】:2016-04-21 04:30:33
【问题描述】:

我的网址中有一个双斜杠(这并不理想)。

所以我的应用在//signup 被点击。

错误信息:

Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET //signin""

还是把它改成/signup

我在第一个位置块(即捕获代理的位置)中尝试了以下内容。

也许有些类似...

location /apps/phpauthentication/1 {      
        rewrite  ^\//(.*)/$  /$1 break;  
        try_files $uri /app_dev.php$is_args$args;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /app_dev.php last;
        }
    }

完整配置:

server {
    listen 80;
    server_name localhost;

    root /srv/http/web;
    index app_dev.php index.php index.html;  

    location /apps/phpauthentication/1 {
        rewrite ^\//(.*)/$ /$uri permanent;
        try_files $uri /app_dev.php$is_args$args;
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /app_dev.php last;
        }
    }

    location ~ ^/(app_dev|config)\.php(/|$) {
        fastcgi_pass app:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param APP_ENV dev;
        include        fastcgi_params;
    }

    location ~ \.php$ {
       fastcgi_pass   app:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       fastcgi_param APP_ENV dev;
       include        fastcgi_params;
    }
}

谢谢:)

【问题讨论】:

  • 完整网址是http://domain.com/apps/phpauthentication/1//signup?
  • 目前来自代理的似乎是//signup,因为这是错误消息。

标签: php regex nginx rewrite


【解决方案1】:

在处理以下内容时,我已经成功地在后端更改了 URI。

     location /apps/phpauthentication/1 {
       rewrite ^(.*)//(.*)$ /$1/$2 permanent;  ##First matches double slash and rewrites
       try_files $uri /app_dev.php$is_args$args;  ##URI is now /apps/1/signup
       if (!-e $request_filename) {
           rewrite ^/(.*)$ /app_dev.php last; ## Matches all request that pass from above
       }

现在浏览器中的 URL 永远不会改变,但后端服务器现在似乎有一个有效的路径。

【讨论】:

  • 感谢以上内容,并且可以接受。我已经设法让它像//signup 一样到达我的后端,但尝试了这个正则表达式regex101.com/r/aD9iB7/1rewrite ^(.*)\/(.*)$ /$2 permanent;,但没有快乐?尝试将其重写为/signup
  • 另外,您对 merge_slashes 的设置是什么?默认为是nginx.org/en/docs/http/ngx_http_core_module.html#merge_slashes
  • 开启了,应该关闭吗?
  • 我的 merge_slashes 设置为 no。它可以在http或服务器中设置。但是,这可能会导致以这种方式设置的其他问题。我只是好奇。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 2012-05-11
  • 1970-01-01
  • 2012-05-08
  • 2016-12-22
  • 1970-01-01
相关资源
最近更新 更多