繁体   English   中英

如何在不同服务器上为apache配置nginx反向代理

[英]How to configure nginx reverse proxy for apache with servername on different server

我有server1和 public IP, server2有本地 IP,我想使用 nginx 反向代理从server1 public IP 访问我在server2上由 apache 托管的应用程序。 这是我的配置。

Apache2 配置

<VirtualHost *:80>
    ServerName ci_app.local
    DocumentRoot /var/www/ci_app/public
    <Directory /var/www/ci_app/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/ACD_error.log
    CustomLog /var/log/apache2/ACD_requests.log combined
</VirtualHost>

Nginx 配置

server {
        listen 8000;
        server_name ci_app.local;

        port_in_redirect on;

        location / {
                proxy_set_header Host $host:$server_port;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://<server2_local_ip>:80/;
        }
}

使用该配置,我在访问 http://<server1_public_ip>:8000 时仅获得默认的 Apache 虚拟主机。

我尝试将 Nginx header 配置更改为

proxy_set_header Host ci_app.local

这允许我访问该应用程序,而不是server1 public IP ajax 将请求发送到 ci_app.local,这是无法从 local.network 外部访问的。

那么,如何正确地反向代理我的应用程序呢?

刚刚通过操作 Header 解决了这个问题。

这可能不是使用 X-Forwarded-Host 和 Host header 的正确方法,但它解决了问题。 这是我的配置。

Nginx 配置

server {
        listen 8000;
        server_name ci_app.local;

        port_in_redirect on;

        location / {

                proxy_set_header X-Forwarded-Host $host:$server_port; #Set request host header and server port to X-Forwarded-Host header
                proxy_set_header Host ci_app.local; #Set target servername to Host Header

                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_pass http://<server2_local_ip>:80/;
        }
}

Apache2 配置

<VirtualHost *:80>
    ServerName ci_app.local

    SetEnvIf X-Forwarded-Host (.*) proxy_host=$1 #Set the X-Forwarded_Host value to env
    RequestHeader set Host "%{proxy_host}e" #Replace the Host header to X-Forwarded_Host value which saved in env

    DocumentRoot /var/www/ci_app/public
    <Directory /var/www/ci_app/public>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog /var/log/apache2/ACD_error.log
    CustomLog /var/log/apache2/ACD_requests.log combined
</VirtualHost>

此配置成功地将我的请求重定向到server2 ,同时修复了错误的 ajax 请求 url。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM