簡體   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