nginx configuration: one location with different server IP - nginx

I have the main app running on a server with IP 127.0.0.1 and the domain is http://myexample.com.
I want to add another service to the app with the url http://myexample.com/service.
However, the service is running on another server with IP 127.0.0.2 with port 5000.
How to make nginx configuration work in this situation?
What I have tried is as below:
server {
listen 80;
server_name myexample.com;
location / {
proxy_pass http://127.0.0.1:3002;
client_max_body_size 100m;
}
location /service {
proxy_pass http://127.0.0.2:5000;
}
location = /50x.html {
root /usr/share/nginx/html;
}
}
when I open myexample.com/service, it returns 404 or 500.

Related

Nginx configuration doesn't work properly on Tomcat

Hi I am facing issue when configuring nginx as proxy server to redirect request to my tomcat server. I have 3 tomcat server running on different machine & different port like this
192.168.51.115:8115
192.168.51.120:8120
192.168.51.130:8130
Now I want to config nginx to pass request to my three server sequentially like this
www.example.com/app1
www.example.com/app2
www.example.com/app3
Real IP: 123.123.123.123
This is my configuration under - site-enabled
server {
listen 80;
server_name example.com www.example.com;
location /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location /app3 {
proxy_pass http://192.168.51.130:8130;
}
}
Note: When i put location directive placing just / then it works but doesn't work on /* like app1,app2 or app3
Can you try using ^~ as modifier in your location block ?
like
server {
listen 80;
server_name example.com www.example.com;
location ^~ /app1 {
proxy_pass "http://192.168.51.115:8115";
}
location ^~ /app2 {
proxy_pass "http://192.168.51.120:8120";
}
location ^~ /app3 {
proxy_pass http://192.168.51.130:8130;
}
}

Ngrok not tunneling properly Nginx

I have my flask application deployed on Nginx over my VM.
Everything is deployed Ok and I can request my apis on http://my.ip.number (I have a public IP)
But when I run Ngrok (I need https and I don't have a domain name to generate a SSL certificate), the URL https//number.ngrok.io shows me the Nginx home page (Welcome to Nginx) instead my webapp.
Why is this happening?
P.D: When I run "curl localhost" I get the Nginx Welcome Page but when I exec "curl -4 localhost" I get my webapp home page
etc/nginx/site-available/myproject
server {
listen 80;
server_name 0.0.0.0;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name 127.0.0.1;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
server {
listen 80;
server_name public.ip;
location / {
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Any request coming in from ngrok, has the Host header set to the ngrok URL. The behaviour of nginx would be to try and match one of the server blocks in your configuration above, and default to the first one if no server_name matches the Host header.
However, I'm guessing there's another configuration file at /etc/nginx/conf.d/default.conf or /etc/nginx/sites-enabled/0-default which has a listen directive with default_server set. That will be catching these requests and serving the "Welcome to nginx!" page.
I suggest you look for that file, and remove it which should solve the issue.
However you could also simplify the above configuration and simply have:
server {
listen 80;
server_name localhost;
location / {
include proxy_params;
proxy_pass http://unix:/home/datascience/chatbot-cima/chatbot.sock;
}
}
Provided there's not another server block hiding somewhere else in the configuration with a directive like listen 80 default_server; then this should catch all requests.
For more info see: How nginx processes a request

Godaddy DNS to AWS EC2

Need help with my web server configuration. I'm using Python Django, Gunicorn, Nginx. DNS Lookup tool shows correct IP. When I go to my domain name it returns Bad Request (400)
#GODADDY RECORDS:
Type Name Value TTL Actions
1. a # 11.222.33.444 600 seconds
2. a www 11.222.33.444 600 seconds
#My SETTINGS.PY FILE:
ALLOWED_HOSTS = ['11.222.33.444']
#NGNIX SETTINGS:
server {
listen 80;
server_name 11.222.33.444 domainname.com www.domainname.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/Portfolio;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/Portfolio/main.sock;
}
}

Nginx Reverse Proxy with Multiple Backend Domains

I have a 2 servers :-
Server 1 : NGINX Reverse Proxy.
Server 2 : NGINX with 5-6 websites ( different domains )
So basically, all users will come to Server 1 which will proxy_pass the traffic to Server 2 and get the response back. Server 1 will also do Caching, WAF etc.
Here is my configuration for Server 1 :-
server {
listen 80;
server_name example.com www.example.com;
location ~* {
proxy_pass http://mysite:80;
}
}
server {
listen 80;
server_name server.com www.server.com;
location ~* {
proxy_pass http://mysite:80;
}
}
In my Server 2, in virtual.conf of NGINX, i have the following config :
index index.php index.html;
server {
listen 80;
server_name example.com www.example.com;
location / {
root /var/www/websites/example/;
include location-php;
}
}
server {
listen 80;
server_name server.com www.server.com;
location / {
root /var/www/websites/server/;
include location-php;
}
}
However whenever i go to http://example.com or http://server.com ( directed via Sever 1 acting as Reverse Proxy ), it shows the Server 2's Default NGINX Page. I am not sure what am I doing wrong. Also is this type of setup a proper way of doing things ?
This is your host problem.
Due to your upstream name is mysite, so the host name in upstream request is mysqsite too.
So the host isn't matched by the backend servers.
You can solve the problem like this by adding the directive before proxy_pass:
proxy_set_header Host server.com

nginx isnt forwarding to static server

I have two servers running in the background, I would like nginx to reverse proxy to both of them.
I want nginx to run on port 80. When a user navigates to http://localhost:80/, he should be forwarded to http://localhost:3501. However I am still seeing the default nginx page at http://localhost:80. I have nginx installed on my localhost, and am testing from the same box.
server {
listen 80;
server_name _;
location ^~/api/* {
proxy_pass http://localhost:8000;
}
location ^~/* {
proxy_pass http://localhost:3501;
}
}
Add upstream:
upstream backend-testserver {
server 127.0.0.1:3501 weight=1 max_fails=2 fail_timeout=30s; # server 1
server 127.0.0.1:3502 weight=1 max_fails=2 fail_timeout=30s; # server 2
}
Add proxy_pass in "server -> location":
location / {
root html;
index index.html index.htm;
proxy_pass http://backend-testserver;
}

Resources