nginx configuration for multiple domain names - nginx

I have just installed nginx and have more than one domain name pointing to same IP. When calling each domain I have to redirect to different applications running on the same machine, each application is running on different port.
For ex, I have app1.domain.com, app2.domain.com & app3.domain.com
so, for app1.domain.com I have to redirect to localhost:<port1>
likewise, app2.domain.com I have to redirect to localhost:<port2>
and app3.domain.com I have to redirect to localhost:<port3>
How do I go about?
Thanks in advance

Well if your application are running on different ports then your nginx conf files should look like this.
upstream app1 {
server 127.0.0.1:port1; #App1
}
upstream app2 {
server 127.0.0.1:port2; #app2
}
server {
listen xxx.xxx.xxx.xxx:80;
server_name app1.domain.com;
access_log /var/log/nginx/log/app1.domain.com.access.log main;
error_log /var/log/nginx/log/app1.domain.com.error.log;
root /usr/share/nginx/html;
index index.html index.htm;
## send request back to apache1 ##
location / {
proxy_pass http://app1;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen xxx.xxx.xxx.xxx:80;
server_name app2.domain.com;
access_log /var/log/nginx/log/app2.domain.com.access.log main;
error_log /var/log/nginx/log/app2.domain.com.error.log;
root /usr/local/nginx/html;
index index.html;
location / {
proxy_pass http://app2;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host app2.domain.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Please let me know if you have any doubts.
Thanks

Related

Nginx returns 404 on proxy_pass

I have the following configuration on Nginx 1.20.1, whenever I try to access test.xxx.com/something/ I get a 404 error. I know there are other similar questions but I already have the / at the end of the proxy_pass so I have no idea what to do. The strange thing is that I have 20 other servers on that configuration, they are all identical but only this one doesn't work. Any idea?
upstream test {
server x.x.x.x:8443;
}
server {
listen 8090;
server_name test.xxx.com;
root /var/www/vhosts/test.xxx.com/;
location / {
access_log /var/log/nginx/access_test.log upstreamlog;
proxy_pass https://test/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_connect_timeout 75s;
proxy_send_timeout 1800s;
proxy_read_timeout 1800s;
}
}

How to add public url to nginx reverse proxy with a access token

I am using a public URL and adding it to my Nginx reverse proxy. I have come across a bad request error when I run my nginx.conf configurations file. I have an access token that also needs to be added
Below is my nginx.conf file.
Any recommendations ?
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
server_name localhost 127.0.0.1;
client_max_body_size 0;
set $allowOriginSite *;
proxy_pass_request_headers on;
proxy_pass_header Set-Cookie;
# External settings, do not remove
#ENV_ACCESS_LOG
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
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_pass_header Set-Cookie;
proxy_set_header X-Forwarded-Proto $scheme;
location /test/ {
proxy_pass https://a***.***.com;
}
}
}
403 ERROR
The request could not be satisfied.
I have fixed the issue by using the Nginx rewrite modules. I have posted a link below.
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
location /test/ {
rewrite ^/test(.*) https://URL$1 break;

Nginx multilayer reverse proxy get wrong redirect in http header

I am using two Nginx machines as reverse proxy,but get wrong Host in the Browser.The detail conf is blow.
In Proxy1(192.168.0.1)
upstream proxy2 {
server 192.168.0.2:8040;
}
server {
listen 8041;
server_name localhost;
root html;
index index.html index.htm index.php;
location / {
proxy_pass http://proxy2;
#Proxy Settings
#proxy_redirect off;
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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
In Proxy2 (192.168.0.2)
upstream backend {
server 192.168.0.2:8041;
server 192.168.0.3:8041;
}
server{
listen 8040;
server_name localhost;
location / {
proxy_pass http://backend;
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;
}
}
In the backend server I just use php redirect / to /auth/login
Then when I visit http://192.168.0.1:8041 , I show go to http://192.168.0.1:8041/auth/login, but it redirect tohttp://192.168.0.1:8040/auth/login , looks like something go wrong with the port passing . Anyone can help me ?

ERR_TOO_MANY_REDIRECTS Nginx

I'm trying to redirect my particular domain to Tomcat where multipe Application is running, but I'm getting "ERR_TOO_MANY_REDIRECTS" ERROR in the browser
My configuration has below
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://localhost:7070/AppName;
proxy_read_timeout 600s;
client_max_body_size 200m;
}
}
Recently I configured my Odoo app to forward all requests via Nginx.
You need to add something like this to your Nginx config:
upstream tomcat {
server 127.0.0.1:8080;
}
server {
listen 80;
server_name www.mydomain.com;
location / {
proxy_pass http://tomcat;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
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_set_header X-Forwarded-Proto https;
}
proxy_read_timeout 600s;
client_max_body_size 200m;
}
}
If this doesn't work, for reference, you may want to check this article: https://www.rosehosting.com/blog/install-odoo-on-a-debian-8-vps-with-nginx-as-a-reverse-proxy/
I hope you'll find this useful.
It is common to set the proxy_redirect directive in the same way as the proxy_pass directive. see for example configure-nginx-with-proxy-pass.
location ~ ^/stash {
proxy_pass http://IP:7990;
proxy_redirect http://IP:7990/ /stash;
}
but I got the ERR_TOO_MANY_REDIRECTS error with this configuration... so i changed it for "proxy_redirect off;" as suggested here, and it solved my problem!
here is the configuration for my gitlab server:
server {
listen 80;
server_name reverseproxy.mydomain.org;
location /gitlab/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host-Real-IP $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://172.xx.xx.xxx:10080;
#proxy_redirect http://172.xx.xx.xxx:10080/ /gitlab/;
proxy_redirect off;
}
}
NB: i also needed to remove the directive "proxy_set_header Host $host;" for my gitlab server, powered by docker-gitlab.

http_sub_module / sub_filter of nginx and reverse proxy not working

I am trying to reverse proxy my website and modify the content.
To do so, I compiled nginx with sub_filter.
It now accepts the sub_filter directive, but it does not work somehow.
server {
listen 8080;
server_name www.xxx.com;
access_log /var/log/nginx/www.goparts.access.log main;
error_log /var/log/nginx/www.goparts.error.log;
root /usr/share/nginx/html;
index index.html index.htm;
## send request back to apache1 ##
location / {
sub_filter <title> '<title>test</title>';
sub_filter_once on;
proxy_pass http://www.google.fr;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Please help me
Check if the upstream source has gzip turned on, if so you need
proxy_set_header Accept-Encoding "";
so the whole thing would be something like
location / {
proxy_set_header Accept-Encoding "";
proxy_pass http://upstream.site/;
sub_filter_types text/css;
sub_filter_once off;
sub_filter .upstream.site special.our.domain;
}
Check these links
https://www.ruby-forum.com/topic/178781
https://forum.nginx.org/read.php?2,226323,226323
http://www.serverphorums.com/read.php?5,542078

Resources