Nginx Reverse Proxy Multiple Applications on One Domain - nginx

like these:
test1.sec.com 192.168.1.8:8001<br>
test2.sec.com 192.168.1.8:8002<br>
test3.sec.com 192.168.1.8:8003<br>
http://www.sec.com/test1/ 192.168.1.8:8001<br>
http://www.sec.com/test2/ 192.168.1.8:8002<br>
http://www.sec.com/test3/ 192.168.1.8:8003<br>
how to config the nginx.conf ?

You haven't provided much information, but based on what you gave, this should work:
server {
listen 80;
server_name test1.sec.com;
location / {
proxy_pass http://192.168.1.8:8001;
}
}
server {
listen 80;
server_name test2.sec.com;
location / {
proxy_pass http://192.168.1.8:8002;
}
}
server {
listen 80;
server_name test3.sec.com;
location / {
proxy_pass http://192.168.1.8:8003;
}
}
Then, for your www.sec.com, you'll need to add separate location blocks to catch the /test/ URIs.
server {
listen 80;
server_name www.sec.com;
location /test1/ {
redirect 301 $scheme://test1.sec.com;
}
location /test2/ {
redirect 301 $scheme://test2.sec.com;
}
location /test3/ {
redirect 301 $scheme://test3.sec.com;
}
#Rest of your config here...
}
Note: You have to specify your test location blocks before your root (/) unless you use a modifier to give them precedence.

Related

nginx proxy_pass external services via locations instead of subdomains

I have setup a docker environment for testing nginx reverse proxy.
Instead of setting up a subdomain for every backend, i would like to have 1 subdomain with a location pointing to the individual backend:
while this works as expected:
server {
listen 80;
server_name dashboard.test.lan;
location / {
proxy_pass http://172.17.0.1:82;
}
}
server {
listen 80;
server_name gitlab.test.lan;
location / {
proxy_pass http://172.17.0.1:83;
}
}
server {
listen 80;
server_name portainer.test.lan;
location / {
proxy_pass https://172.17.0.1:9443;
}
}
...this does not work:
server {
listen 80;
server_name proxy.test.lan;
location /dashboard {
proxy_pass http://172.17.0.1:82;
}
location /gitlab {
proxy_pass http://172.17.0.1:83;
}
location /portainer {
proxy_pass https://172.17.0.1:9443;
}
}
Maybe you guys can give me a hint or explanation why this does not work.

How to redirect traffic nginx in dynamic mode

In my nginx server , I want to do this:
mydomain.com/api/xxxx
redirect to mynewdomain.com/api/testing/xxxx
I try to do by different ways but no ones works for me, always return a 502 error;
First way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
return 301 https://mynewdomain.com/api/testing/$uri;
}
}
Second way
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/(.*) {
proxy_pass https://mynewdomain.com/api/testing/$args_v;
}
}
And I tryed to only redirect to specific direction and this works, but i don't want to hardcode all petitions to redirect,
Static redirect
server {
listen 80;
server_name mydomain.com;
# Nginx conf
location /api/ {
proxy_pass https://mynewdomain.com/api/testing/redirect1;
}
Any help for make this dynamic?
location ~ ^/api/(.*) {
return 301 https://mynewdomain.com/api/testing/$1;
}
Try this, because $uri also includes /api.

How to correctly set root for nginx server?

I must set root in server, not in location. If I set it this way:
server {
listen 80;
server_name www.mydomain.com;
location / {
root /opt/html;
index index_sc.html;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
}
Then I can access /imgproxy content without problem. But if I move the root out of location like below, /imgproxy will cause 404 error:
server {
listen 80;
server_name www.mydomain.com;
root /opt/html;
location / {
index index_sc.html;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
}
I want to know how exactly can I set root in server and still has location /imgproxy/ work? Thanks!
I cannot reproduce this behaviour, but you can explicitly handle the /imgproxy URI by adding another location block:
root /opt/html;
location / {
index index_sc.html;
}
location = /imgproxy {
return 301 /imgproxy/;
}
location /imgproxy/ {
proxy_pass http://127.0.0.1:8080/;
}
Ensure that you clear the browser cache between changes to the nginx configuration.

Nginx: multiple locations on same server

This is my nginx conf file.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:9000;
}
location /some/directory {
proxy_pass http://localhost:8998;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8999;
}
}
For some reason example.com and example2.com are working, but example.com/some/directory is not.
localhost:9000 & localhost:8999: are harp.js sites, they have they own routing, and work properly, both locally and on the proxy-ed domains (example.com & example2.com).
localhost:8998: is a golang api, it works locally and also if I access example.com:8998 or example2.com:8998.
Is there something wrong with the conf?
EDIT: added more info to the question.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:9000/;
}
location /some/directory {
proxy_pass http://localhost:8998/;
}
}
server {
listen 80;
server_name example2.com;
location / {
proxy_pass http://localhost:8999/;
}
}
Try this, adding the last / in the proxy_pass's should work.

nginx duplicate location "/" error

I want to redirect to https when scheme is http and location is /
server {
listen 80;
server_name bla;
location / {
return 301 https://bla;
}
include fs.inc;
}
Now, problem is: fs.inc includes anothes "location /" and even if this is never called the nginx configuration test fails with error duplicate location "/" in fs.inc:1.
How can I solve that?
You need another server block:
server {
listen 443;
server_name bla; # make sure this is the same
# add your ssl specific directives here
location / {
alias /var/www/secured/;
}
}
server {
listen 80;
server_name bla;
return 301 https://$request_uri;
}
This is a global redirect

Resources