【问题标题】:Nginx&Apache2: If / is forgotten, apache redirects to its local portNginx&Apache2:如果忘记了/,apache重定向到它的本地端口
【发布时间】:2016-11-23 16:40:18
【问题描述】:

我在 Nginx 反向代理后面设置了一个 Apache 网络服务器,用于处理 SSL 和子域。

它几乎适用于所有事情,除了如果我忘记了 url 末尾的最后一个 /(例如 https://vps.com/url 末尾没有 /),Apache 会将我重定向到它的本地端口(Apache只监听 127.0.0.1,端口 4000,如果我忘记了 /,它会将我重定向到 https://vps.com:4000/url/,这显然不起作用)。

这是我的 nginx 配置:

server {

    listen 443 ssl;

    // SSL Certificate location & such

    access_log /var/log/nginx/apache-ssl-access.log;
    error_log  /var/log/nginx/apache-ssl-error.log;

    server_name <my remote url>;

    location ~ {
            proxy_set_header                X-Real-IP               $remote_addr;
            proxy_set_header                X-Forwarded-For $remote_addr;
            proxy_set_header                Host                    $host;
            proxy_pass              http://127.0.0.1:4000;
    }

    location ~ /\.ht {
            deny all;
    }
}

这是处理服务的 apache 配置文件:

<VirtualHost 127.0.0.1:4000>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    LogLevel warn ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

我的服务器安装在 Debian8 机器上,运行以下版本:

  • Apache/2.4.10
  • nginx/1.10.2

我不知道什么是重定向或如何防止这种情况发生,知道吗?

【问题讨论】:

  • 问题是?
  • 尝试用proxy_pass http://vps.artemix.xyz:4000/;替换proxy_pass http://127.0.0.1:4000; ?

标签: apache redirect nginx


【解决方案1】:

看起来 Apache 正在发出 3xx 响应,以在以目录名称结尾的 URI 中附加一个斜杠。

间接解决方案是使用nginx 中的proxy_redirect 指令将重定向从内部地址转换为外部地址。

只需将您的location 块更改为前缀位置(请参阅this document)并将/ 附加到您的proxy_pass 语句可能会起作用,因为默认操作可能足以解决问题:

location / {
    ...
    proxy_pass              http://127.0.0.1:4000/;
}

如果需要更具体的解决方案,可以将多个proxy_redirect 语句添加到location 块中。

要找到您需要的确切映射,您应该使用curl(或合适的浏览器工具)分析 HTTP 3xx 响应中的 Location 标头,该标头将 URL 设置为内部地址。

请参阅this document 了解更多信息。

【讨论】:

  • 我将 location ~ 更改为 location / 并在代理通过行的末尾添加了 /(在 ; 之前)。当我尝试访问时,它也是如此,当尝试访问https://vps.artemix.xyz/class 时,Location 标头为http://vps.artemix.xyz:4000/class/
  • 尝试将:proxy_redirect http://vps.artemix.xyz:4000/ https://vps.artemix.xyz/; 添加到 location 块中。
  • 在 nginx 位置 / 中添加了 proxy_redirect http://vps.artemix.xyz:4000/ https://vps.artemix.xyz/; proxy_pass http://127.0.0.1:4000/;。它仍然这样做。 (当前有效)
  • 你重启了nginx?
  • 是的,systemctl restart nginx 每次
猜你喜欢
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2018-10-31
  • 2021-04-09
  • 2018-09-11
相关资源
最近更新 更多