redirect - 如何让 nginx 代理停止重定向到 root?

标签 redirect nginx proxy

我有一个节点应用程序作为子目录中 nginx 上的代理运行。我在重定向指向应用程序本身的不同部分时遇到问题。它们总是重定向到根目录而不是代理的子目录。

示例:

如果我的应用代理位于 https://example.com/myApp/ 并且它重定向到 /admin/ 我希望页面重定向到 https://example.com/myApp/admin/ 不是 https://example.com/admin/

<小时/>

这是我的配置的相关部分:

  location /myApp {
    rewrite /myApp(.*) $1 break;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:3030;
    proxy_redirect http://localhost:3030/ /myApp/;
  }

我还尝试将 proxy_redirect 设置为 (^|^http://localhost:3030)//myApp/;

<小时/>

这是该域的完整配置文件:

upstream php-handler {
  #server 127.0.0.1:9000;
  server unix:/var/run/php5-fpm.sock;
}

## HTTP
server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;
  server_name example.com www.example.com;
  return 301 https://$server_name$request_uri;
}

## HTTPS
server {
  listen 443 ssl;
  server_name example.com;

  ssl_certificate /etc/ssl/www.example.com.crt;
  ssl_certificate_key /etc/ssl/www.example.com.key;

  add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload;";
  add_header X-Content-Type-Options nosniff;
  add_header X-Frame-Options "SAMEORIGIN";
  add_header X-XSS-Protection "1; mode=block";

  root /var/www/example.com/;
  index index.html index.php;

  client_max_body_size 500M;
  fastcgi_buffers 64 4k;
  gzip on;

  # Website
  location / {
    try_files $uri $uri/ =404;

    location ~ \.php$ {
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_index index.php;
      include fastcgi_params;
    }

  }

  # My App
  location /myApp {
    rewrite /myApp(.*) $1 break;

    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://localhost:3030;
    proxy_redirect http://localhost:3030/ /myApp/;

  }

最佳答案

好吧,我自己解决了。

tl;博士:

这有效:

proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;
<小时/>

第一个选项:

proxy_redirect / /myApp/;

我猜这有效,因为应用程序正在重定向到 /admin/不是http://localhost:3030/admin/ 。然而,这并没有提供我所寻求的灵 active 。

<小时/>

第二个选项:

使用正则表达式

所以当我尝试使用这样的正则表达式时,我最初犯了一些错误:

proxy_redirect (^|^http://localhost:3030)/ /myApp/;

首先,如果您要使用正则表达式,则需要以 ~ 开头。区分大小写或 ~*不区分大小写。

其次,当您使用正则表达式时,它显然会替换完整路径,而不仅仅是匹配的部分。例如,如果您要像这样设置 proxy_redirect:

proxy_redirect ~/(a|b)/ /myApp/;

如果您随后重定向到 /a/some-subdirectory/重写将导致 /myApp/不是/myApp/some-subdirectory/ 。为了使这项工作有效,您需要捕获路径的末尾并将其插入到重写的末尾,如下所示:

proxy_rewrite ~/(a|b)/(.*)$ /myApp/$2;

注意:我在文档中找不到有关此行为的任何信息。我只是根据自己的尝试和错误的结果得出此结论。

<小时/>

所以我最终的 proxy_redirect 看起来像这样:

proxy_redirect ~(^http://localhost:3030|^)/(.*)$ /myApp/$2;

关于redirect - 如何让 nginx 代理停止重定向到 root?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37195810/

相关文章:

apache - 重定向到域的不同 Apache 服务器,但保持 URL 不变

c# - 我可以在析构函数中封装 wcf 代理 close() 吗?

c++ - Windows - 在不修改 etc/hosts 的情况下将域请求重定向到本地主机

php - Htaccess - 验证是否 Cookie ISSET

redirect - 仅对网站上的某些内容使用 301 最终会删除这些页面?

javascript - 从自定义 URL 重定向到 WhatsApp 应用程序

elasticsearch - 无法连接到我的代理 elasticsearch 节点

带或不带尾部斜杠的 nginx 位置

redirect - nginx redirect :/*/to/*. html (add .html to url)

c# - 检测默认网络浏览器的代理设置