I'm new to NGINX and I'm trying to setup a reverse proxy where I make several internal sites available from outside. My goal is to use https:// nginx.company.com/sitea to access internal webserver A, https:// nginx.company.com/siteb to access internal webserver B, etc. So one external hostname and port to proxy for several internal services based on URL/location.
Right now I am able to access webserver A via https without any problems (Apache), that one works completely fine (location /sitea). However all other internal webservers (most of them IIS, lots of PHP and ASP) I've tried to make available are problematic, most of them give me a 404 error, while some present an empty page but you can see the site's source code in the browser, yet again others only appear partially. I've been through several threads with similar issues and tried out many of the offered solutions, but none helped in my case.
Let's focus on the 404 one first. My configuration file looks like this:
server {
server_name nginx.company.com;
listen 443;
ssl_certificate /etc/nginx/cert.pem;
ssl_certificate_key /etc/nginx/key.pem;
ssl on;
ssl_session_cache builtin:1000 shared:SSL:10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
location /siteA {
proxy_set_header Host $host;
proxy_pass http://siteA.company.com;
}
location /siteB/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://siteB.company.com/;
}
}
site A works fine, site B will give me a 404 error. The reason can be seen in the error log: It's always trying to access /usr/share/nginx/html/ for that content, but of course it's not in that local folder of the nginx machine. So the error seems to be that it's treated as local web content rather than something to be proxied.
When you access the URL for site B, the webserver will instantly refer you to a specific path, like http:// siteB.company.com/webapp/something/login.php. The error log shows that nginx thinks it should access /usr/share/nginx/html/webapp/something/login.php/, which of course doesn't exist.
Things I've tried to fix this:
- remove the proxy_set_ options
- configure it identically to site A
- set proxy set header host to siteB.company.com
- remove the / after location /siteB
- remove the / after the target URL
- enter the full target URL
- add ^~ in front of the location
- try the URL rewrite where "/siteB" is removed
- several snippets I've, quite honestly, already forgotten and didn't understand
None of it worked, I still don't get to proxy that site and receive an error 404 instead (from nginx, not from the webserver). How should I change the settings for site B in order to access that one as well?
By the way, when I set up an additional "server" in the config that listens on a different port and uses location / it will work, so the problem comes with the constellation of dividing traffic by URL instead of by hostname or port.
Thanks in advance