Use Nginx as Reverse Proxy for multiple servers
Your problem is that you are using proxy_pass
inside server
block, which is not allowed. Try using:
server {
server_name Subdomain.domain.eu;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://10.0.2.33:80;
}
}
inside your server
block. Proxy options cannot be set on server
level, as nginx documentation tells.
The other problems in your log happen because you have somehow your nginx starting up as a regular user, although it is supposed to start up as root
.
This thread solved my problem, but I thought it would be useful for others to have a completed configuration to see. The following configuration will reverse proxy for hostnames app1.local and app2.local, where app1 gets forwarded to another application listening on port 3300 and app2 is forwarded to a different application listening on port 3000. It is in a file here /etc/nginx/sites-available/two-applications.conf
server {
server_name app1.local;
location ~ {
proxy_pass_header Authorization;
proxy_pass http://localhost:3300;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
client_max_body_size 0;
proxy_read_timeout 36000s;
proxy_redirect off;
}
}
server {
server_name app2.local;
location ~ {
proxy_pass_header Authorization;
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
client_max_body_size 0;
proxy_read_timeout 36000s;
proxy_redirect off;
}
}
Also, those hostnames are made up and need to be in /etc/hosts
as follows to have them work:
127.0.0.1 app1.local
127.0.0.1 app2.local
For the sake of completeness (as setup on Ubuntu Linux), this file lives in /etc/nginx/sites-available/two-applications.conf
and is symlinked into /etc/nginx/sites-enabled/two-applications.conf
The filename and symlink name can be anything of course. After updating that running sudo service nginx reload
will pick up the change.