NGINX config: issue to proxy_pass a folder to a subdomain - nginx

I want to display the subdirectory /obvious on the subdomain obvious.example.com
I added a CNAM record in Cloudflare to create the subdirectory, pointing to the regular app
I added the following to my NGINX config:
server {
listen 80;
server_name obvious.example.com;
location / {
proxy_pass https://www.example.com/obvious$uri;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect off;
}
client_max_body_size 4G;
keepalive_timeout 10;
}
However, I get a 502 Bad Gateway error. Could you help me pinpoint what I am doing wrong here? Thanks.

Related

Cannot access local subdomain

First of all, Im on mac os. And I modified /etc/hosts to look like this:
127.0.0.1 locaserver.com
127.0.0.1 api.locaserver.com
127.0.0.1 images.locaserver.com
As Im running nginx (installed with brew), I modified /opt/homebrew/etc/nginx/nginx.conf to have this:
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 20M;
sendfile on;
keepalive_timeout 65;
error_log /var/log/nginx/error_log;
server {
listen 8080;
server_name images.locaserver.com www.images.locaserver.com;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_pass http://localhost:8083;
}
}
server {
listen 8080;
server_name locaserver.com www.locaserver.com;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
}
}
server {
listen 8080;
server_name api.locaserver.com;
location / {
client_max_body_size 20M;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-for $remote_addr;
proxy_pass http://localhost:8081;
}
}
include servers/*;
}
Im runinng a react app on port 3000. And a nodejs on port 8081 (API). Before I added the new entry for images.locaserver.com everything worked as expected.
So when I went to locaserver.com:8080 I saw the react app. And when react app made requests to API, it did it at http://api.locaserver.com:8080.
So when I created a new node-app (images.locaserver.com on port 8083), the API is still working. However, the only way to access the images.locaserver.com is by going directly to the port number:
http://localhost:8083/
or even
http://images.locaserver.com:8083/
both work
What is wrong? I tried to move the entry for images.locaserver.com to be the first server, but it did not help.
When I go to http://images.locaserver.com:8080 then the react app takes over showing a page.
I looked into logs but no errors show up, as it is actually serving the page (the react one though).
Restarting the computer solved the problem.

nginx reverse proxy to apache-wordpress works but proxy_pass to external url fails

I have a nginx reverse proxy setup for apache wordpress which works fine. However based on location need to redirect to an external url which fails. Please check the below config. Is this a valid setup ?
https://platform.com/ - this works - also any subsequent wp pages also works
https://platform.com/pen - this needs to redirect to https://abcdef.com - this doesn't work - 404 page load error Any help ?
server {
listen 443 ssl default_server;
listen [::]:443 default_server;
server_name platform.com;
server_tokens off;
root /var/www/html/def/public/;
index index.php;
ssl on;
ssl_certificate /tmp/fgh.crt;
ssl_certificate_key /tmp/fgh.pem;
access_log /var/log/nginx/access2.log;
error_log /var/log/nginx/error2.log;
location / {
proxy_set_header X-Forwarded-Proto $scheme;
try_files $uri #apache;
}
location #apache {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location ~[^?]*/$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:8080;
}
location /pen {
proxy_pass https://abcdef.com;
}
}
After changing the server name (wordpress site) from http prefix to www prefix, proxy pass re directions worked. Had to redirect all http https server blocks to www server block in nginx config
What you are doing is a proxy_pass to https://abcdef.com , not a redirect. if you meant a redirect the code is :
location /pen {
return 301 https://abcdef.com;
}
If it's not a definitive redirect, use 302 instead of 301, so is not cached (for tests is much better).
The reason the 404 is given is because you are accessing the https://abcdef.com with a request with the host/url https://platform.com/pen
If the destiny server is not prepared to recive this whole url, it returns 404, as /pen is not found.

NGINX defaulting to welcome page on Second domain name pointing to node server

I have a Single Page Application running on a node server serving angular at www.xxx.com. This is currently working.
I am trying to server a second Node application named www.yyy.com however when I set up the NGINX server blocks it is defaulting to the NGINX welcome page.
www.xxx.com NGINX server block (Which is working fine):
server {
listen 80;
listen [::]:80;
server_name xxx.com.au www.xxx.com.au;
return 301 https://xxx.com.au$request_uri;
}
server {
listen 443;
server_name xxx.com.au www.xxx.com.au;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
}
ssl on;
ssl_certificate /etc/letsencrypt/live/xxx.com.au/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.com.au/privkey.pem;
}
www.yyy.com Server block: (Currently only serving welcome page)
server {
listen 80;
server_name yyy.com www.yyy.com;
location /site {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3002/;
proxy_redirect off;
}
}
I have all the DNS set up and the host names set up on my droplet as well. I am using Vultr running Ubuntu if that helps.
I have added both via symbolic link to Sites-available and the line is present in the conf file.
EDIT: As Henry pointed out I was server /site
location /site {
You're serving the app at /site and not /.
You can map different different config blocks to different URLs, so you could e.g. route /example to a different node server if you wanted.
Replacing location /site { with location / { as for your working block will serve your node application at the root. With no configuration for the root node nginx routes it to its default page.

Getting a nginx reverse proxy to work with plex

Since a recent update of plex, my reverxe proxy for plex stopped working. I tried searching all around, but I didn't find much info. Listed below is the config file, does anyone have plex and know what goes wrong? I simply get served with a 404 not found page
server {
listen 80;
if ($http_referer ~* /plex/) {
rewrite ^/web/(.*) /plex/$1? redirect;
}
root /var/www;
location /plex/ {
proxy_pass http://127.0.0.1:32400/web/;
}
location /plexapi/ {
proxy_pass http://127.0.0.1:32400/;
}
}
Here is mine which works perfectly:
server {
listen 443;
server_name plex.mydoamin.com ;
satisfy any;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
location = / {
rewrite ^ /web/;
proxy_redirect http:// $scheme://;
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 $scheme;
}
location / {
proxy_pass http://192.168.1.14:32400/;
proxy_redirect http:// $scheme://;
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 $scheme;
index index.html index.htm;
}
}
Some things to check:
Run `sudo netstat -nlp | grep ':32400' to make sure there is still something running on the expected port.
From withing the machine running Nginx and Plex, try connecting directly to the URL. Both of these should work: `curl http://127.0.0.1:32400/ and curl http://127.0.0.1:32400/web/ ;
If any of these tests fail, the problem with Plex and not Nginx. In which case, you post your Plex configurations.
It's also a good idea to check the Changelog for Plex. Are there mentions in the Changelog if changes that might be problematic for you since the last time you updated Plex?

NGINX - proxying to a different Wordpress site while retaining URL

I currently have two WORDPRESS websites sitting behind an NGINX proxy cache:
htxtp://local.example.com
htxtp://local.example.org
I want to access a URL from the first site but serve it from the second site whilst not losing the URL structure of the first (to allow website2.com to see the website1.com cookies).
For example:
I want:
htxtp://local.example.com/somepage/
To proxy the page built at:
htxtp://local.example.org/somepage/
BUT I don't want the URL to BE htxtp://local.website2.com.
My NGINX config is as follows:
server {
listen 80;
server_name local.example.com;
access_log logs/local.example.com.access.log;
error_log logs/local.example.com.error.log;
location /somepage {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host local.example.org;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host local.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Any suggestions? I am trying to work out where the actual redirect is happening.

Resources