I have server1 with public IP and server2 with local IP, I want to access my app hosted by apache on the server2 from server1 public IP using nginx reverse proxy. Here's my config.
Apache2 config
<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 config
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/;
}
}
Using that configuration I only get the default Apache virtualhost when accessing the http://<server1_public_ip>:8000.
I've tried changing the Nginx header config to
proxy_set_header Host ci_app.local
Which allow me to access the app but rather than server1 public IP the ajax sent the request to ci_app.local which unaccessible from outside the local.network.
So, how to correctly reverse proxy my app?
Just fixed this by manipulating the Header.
This maybe not a proper way for using the X-Forwarded-Host and Host header but it fixed the problem. Here's my config.
Nginx config
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 config
<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>
This config successfully redirect my request to server2 while fixed the wrong ajax request url.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.