Nginx 1.18 (on Ubuntu 20.04) proxy_pass not working - nginx

I'm new to Nginx proxy_pass, I want to convert app1 subdomain to path of app1. and url should be subdomain. I tried again and again but no luck. I'm stuck in this matter for two days.
server {
listen 80;
server_name app1.example.com;
location / {
proxy_pass http://example.com/app1$request_uri;
proxy_set_header Host app1.example.com;
}
}
Output : is 502 Bad gate way.
But when I access manually the http://example.com/app1 , it showing fine. Can't accessing from proxy_pass.
I tried many ways from the blog internet but not solved.
Thanks in advance.

Related

Forward a port with NGINX

I am trying to forward an python application to a directory. I know I worded this badly, let me try and explain it better
What I currently have:
NGINX on port 80
Static HTML files on /var/www/html
Python API on port 5500
localhost:5500/apiEndpoint (for example)
How can I change it so if I go to
localhost:80/api/apiEndpoint
it gives me localhost:5500/apiEndpoint
?
Thank you for your help :)
If you want to do a mask of that location, you can try:
server {
listen 80;
listen [::]:80;
root /var/www/html;
server_name domain.com;
location ~ ^/api/apiEndpoint/(.*)$ {
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:5500/apiEndpoint/$1;
}
}
Hope i understood your question correctly.

nginx upstream subdomain on the same server

I am configuring Nginx load balance with Nginx upstream module, configuration as follow:
upstream load {
server loadapi.example.com;
server loadapi.anotherdomain.com down;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://load;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name loadapi.example.com;
root /disk/projects/load/loadapi;
index index.html index.htm index.shtml index.php;
...
...
error_page 404 /404.html;
}
Notice that the api.example.com and loadapi.example.com are on the same server. loadapi.anotherdomain.com is resolved to another server which provides the same service.
Everything works fine with loadapi.anotherdomain.com, which are on another server.
But when I use the loadapi.example.com as the backend, it seems that Nginx cannot handle it correctly. I can get my service up and running on loadapi.example.com. But it is not working with the upstream.(look like Nginx cannot resolve the subdomain name correctly).
any advice? thx in advance.
nginx uses the Host header to determine which server block to use to process a request.
When the request passes through the proxy_pass http://load; statement, the Host header is set to the value load by default.
To make nginx choose the server block containing the server_name loadapi.example.com; statement, it either needs to be the default_server server, or include the name load in its server_name, or set the Host header using:
proxy_set_header Host loadapi.example.com;
Of course, using upstream for load balancing means that both servers receive the same value for the Host header, and must both respond correctly to it.
See this document for more.

Not able to change NGINX port

The problem I'm having trouble with is that I can't access remotely unless I use port 80 and I want to use a different port.
Here's the NGINX configuration I'm using. This will work on port 80. However, if I change
listen 80;
to
listen 6000;
it does not work when accessed from outside the local machine.
In other words, curl 127.0.0.1:6000 on the machine works. However trying to visit externally with 184.169.100.100:6000 does not work. (Pretending that's my public IP address.) It gives me a "site can't be reached" error in Chrome.
I've checked the security settings to make sure port 6000 is open. It's an AWS EC2 instance.
Optional note to put things in context: Overall what I'm trying to do is set up two different servers on one machine, each accessible from a different port, and each running it's own set of python workers. As a first step, I just want to make sure I can change the port by which a server is accessed, however, I'm not even able to do that yet and still access it externally.
ubuntu#ip-172-31-9-113:/etc/nginx/conf.d$ cat flask.conf
upstream gunicorn_server {
server localhost:8080 fail_timeout=0;
}
server {
listen 80;
server_name 184.169.100.100;
root /home/ubuntu/www;
client_max_body_size 4G;
keepalive_timeout 5;
proxy_read_timeout 900;
location / {
try_files $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_redirect off;
# pass to the upstream gunicorn server mentioned above
proxy_pass http://gunicorn_server;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Any help is appreciated.
this happen for firewall.
ubuntu:
if your ufw is enabled:
sudo ufw allow 6000
in centos disable selinux (not recommended. search for selinux config for allow port)
Is 6000 port open ?
Check in inbound port rules.
And then try hitting sudo ufw allow 6000 and check if you can access with 6000 also.
(this question is old, but still answering because if someone is facing same issue this answer may help them)
Try to use this command:
firewall-cmd --zone=public --add-port=6000/tcp --permanent
Answering my own question:
I tried using port 6001 instead of 6000 and then it worked. I could access it from outside. I can't say why, but there must have been an issue with 6000 for my particular case.

Nginx - Direct sub-folder to different server

I have a PHP/MySQL website hosted on an Ubuntu 16.04 server, for example on the domain www.website.com, and I would like a "sub-folder" of that same domain, to be hosted on a separate server.
For example:
http://www.website.com is hosted on server IP 1.1.1.1
And I would like
http://www.website.com/another-site/ to be hosted on server IP 2.2.2.2
Both websites are PHP/MySQL, and both servers are Ubuntu 16.04 and I am using Nginx server blocks to manage the domains.
I have had some success with the following set up on 1.1.1.1, but it doesn't seem to work completely and images and css files on the site 404:
location /another-site {
proxy_pass http://2.2.2.2:80;
proxy_set_header Host another-site.dev;
proxy_set_header X-Forwarded-For $remote_addr;
}
And then on the 2.2.2.2 server:
server {
listen 80;
server_name another-site.dev;
root /var/www/another-site;
index index.php;
...
Am I on the right lines, or is there a simpler way of achieving this? I don't want to resort to having both sites on the same server if I can help it.
proxy_pass http://2.2.2.2:80; requires a slash at the end proxy_pass http://2.2.2.2:80/; or it only loads the index.

NGINX passing requests to Pylons and relative URLs

I have NGINX running on port 8080. I have the following setup in my NGINX conf file.
server {
listen 8080;
server_name domain.com;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
proxy_pass http://127.0.0.1:80;
proxy_redirect http://127.0.0.1:80 http://domain.com;
}
These rules work correctly as far as I can tell. The only issue I run into is when Pylons gets a request for a relative URL it is using http://127.0.0.1/linkto/something instead of http://domain.com:8080/linkto/something. I believe I am missing something in my Pylons configuration, if you have any advice or need additional information just let me know. Thanks in advance for any assistance on this.
By default, proxy_pass uses the hostname from the directive (127.0.0.1 in your case) as the Host: header for its request. You probably just need to add proxy_set_header Host $http_host; to have it pass through the original Host header to your backend.

Resources