Welcome! Log In Create A New Profile

Advanced

Help needed--reverse proxy to different internal hosts based on url

Posted by RockTheHouse 
Help needed--reverse proxy to different internal hosts based on url
February 11, 2015 07:13PM
Greetings!

I'm running nginx 1.7.9 on Windows 7 in order to provide secure access to two webcams. I have it working with one of the webcams, but what I'd like is to be able to do is, with nginx still listening only on 443 and using the same domain (whatever.example.com), proxy to the second camera by changing the url--by going to https://example.com/camb for example.

Here is what I believe is the relevant portion of my current nginx.conf configured for just the one camera and below are some other things I've already tried that have not worked.

server {
listen 443;

ssl on;
ssl_certificate ssl/myssl.crt;
ssl_certificate_key ssl/myssl.key;
location / {
proxy_pass http://internalip:port;
proxy_set_header host example.com;
}
}

I have tried adding a second location with /cam2 using the same config as in the location for "/", changing only the internal ip and port for the second location so that it points to the second camera. When I do that, I get a 404 error when attempting to access /cam2.

I also tried the below but still get a 404 error. I don't have anything else installed on the Windows box other than nginx--meaning no apache, IIS, perl, etc. I'm not sure if anything else is needed for the nginx rewrite module to function or not. I'm very much a noob at this.

location /cama {
rewrite ^/cama(.*) /$1 break;
proxy_pass http://internalipcama:port;
}

location /camb {
rewrite ^/camb(.*) /$1 break;
proxy_pass http://internalipcamb:port;
}

Any help would be appreciated. Thanks!
Re: Help needed--reverse proxy to different internal hosts based on url
February 12, 2015 06:32AM
See http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
or rewrite the location rewrite /cama/([^/]+) /$1 break;

---
nginx for Windows http://nginx-win.ecsds.eu/
Re: Help needed--reverse proxy to different internal hosts based on url
February 14, 2015 10:35AM
Thanks for your help. I'm still struggling with this. I fear I am making it too complicated somehow. I've tried the below but still get a 404 error. Also, while I've got the rewrite logging option enabled, I do not see any of the rewrites being logged in the error log, which makes me wonder if something else is amiss.

location /cama {
rewrite_log on;
rewrite /cama/([^/]+) /$1 break;
proxy_pass http://internalip:port;
}

I've also tried using multiple subdomains (which I'd prefer not to do) using the below. In this case, I don't get any errors but I am always proxied to the internal IP and port referenced in the first server entry regardless of which subdomain I go to.

server {
listen 443;
server_name host1.example.com;
ssl on;
ssl_certificate ssl/myssl.crt;
ssl_certificate_key ssl/myssl.key;

location / {
proxy_set_header Host host1.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://internalip:port;

}
}

server {
listen 443;
server_name host2.example.com;
ssl on;
ssl_certificate ssl/myssl2.crt;
ssl_certificate_key ssl/myssl2.key;
location / {
proxy_set_header Host host2.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://internalip2:port;
}
}
Re: Help needed--reverse proxy to different internal hosts based on url
February 14, 2015 11:28AM
If you download prove05.zip of my site and check out the file nginx-win-prove.conf;

location /ajptest1/ {
[.............]
rewrite /ajptest1/([^/]+) /$1 break;
ajp_pass tomcats/; # or pass directly to IP
}

I have in my debug log:

2015/02/13 12:09:34 [notice] 1632#2812: *61 "/ajptest1/([^/]+)" matches "/ajptest1/index.html", client: 127.0.0.1, server: localhost, request: "GET /ajptest1/index.html HTTP/1.0", host: "127.0.0.1:880"
2015/02/13 12:09:34 [notice] 1632#2812: *61 rewritten data: "/index.html", args: "", client: 127.0.0.1, server: localhost, request: "GET /ajptest1/index.html HTTP/1.0", host: "127.0.0.1:880"

So I know it works.

---
nginx for Windows http://nginx-win.ecsds.eu/
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 330
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready