rewrite nginx subdirectories to local ports - node.js

Hi i´m trying to host several sites on one server using nginx. Therefore i want to do the following:
calling www.domainname.com/site1
shall forward to http://localhost:9800
calling www.domainname.com/site2
shall forward to http:/localhost:9801
the local sites are hosted by nodejs with express, so rewriting the paths are necessary.
What i´ve done so far is the this:
location /site1 {
rewrite ^/site1/?(.*)$ /$1 break;
proxy_pass http://127.0.0.1:9800;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
but this only works partly: The stylesheets etc. will not loaded and trying to call
www.domainname.com/site/login
is not working.
Can someone help me writing the correct rewrite ?
Thanks in advance

Related

How do i configure nginx (and strapis) to correctly serve two strapi instances on the same server?

i want to host two small websites, both made with strapi backend and react frontend, on my server which is a digital ocean droplet.
I already configured nginx in order to work for one of the websites and everything is working correctly. I can access strapi from site1.com/dashboard and my queries point to site1.com/api/graphql. I followed some tutorials for that.
Here are the nginx files i added:
/etc/nginx/sites-available/site1.com:
server {
listen 80;
listen [::]:80;
root /var/www/site1.com/react;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name site1.com www.site1.com;
location /api/ {
rewrite ^/api/(.*)$ /$1 break;
proxy_pass http://strapi;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $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;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
}
location /dashboard {
proxy_pass http://strapi/dashboard;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $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;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_pass_request_headers on;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
}
}
file /etc/nginx/conf.d/upstream.conf:
upstream strapi {
server 127.0.0.1:1337;
}
What i now want to do is to deploy another website to the same server and configure nginx to serve it as well (on another domain, for example site2.com).
So i added the nginx virtual server file for the second website, copying the first one and changing the domain name and root directory.
The site 2 frontend is now working correctly and accessible on its domain.
However when i start the site 2 strapi instance it says the port 1337 is already in use (obviously, it is used by site 1 strapi instance that is running with pm2). So i changed the port in strapi config to be 1338 (is it ok?) but now i don't know what do to in nginx in order to serve the two different strapi instances on different domains.
The hostname you are using for the proxy_pass directive is the upstream name you defined in the separate config file. This is basically an indirection to one or more real backends. In your case that's the application running on port 1337 on the same machine. It could also be a list of external domains where nginx takes care to distribute the load to.
Your approach with additional virtual hosts already looks good. The frontend that already "works" under site2 is probably the old instance served under the new domain if your proxy_pass directive still points to http://strapi for site2 (which probably still resolves to `localhost:1337).
As you already mentioned, a second instance of the backend needs to run on a different port. The port number you use is not really important as you will control this with your upstream configuration. Just make sure to not use a port number below 1024 (which requires root permissions), don't conflict with other processes on the system (you will notice) and as best practice, don't use port numbers that are default for some other protocols (maybe check this list).
To fix your setup, just define a second upstream like the first one but pointing to the new url e.g. localhost:1338 and then reference this new name in the proxy_pass directive of site2.
Technically, with just one backend per upstream, you could also skip the upstream part completely and reference the URL directly in the proxy_pass directives, but using a shorthand name can also support readability of your configuration.

Nginx - Reverse proxy to an already reverse proxied location or double reverse proxy?

I am using nginx on centos 7.
I am reverse proxying a remote nodejs server on the same LAN to the nginx root / as per the below:
location / {
proxy_pass http://192.168.1.104:3000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
This works fine for serving my website - all external requests on port 80 are rewritten to use https which has been configured in nginx e.g nginx forwards any incoming http requests to https and deals with rewrites and forwarding so that the nodejs content is served over ssl even though ssl hasn't been configured within the node application.
e.g my site can be accessed at https://example.com
I now want to reverse proxy another nodejs app so that it appears at a location which is prefixed with https://example.com e.g: https://example.com/node2/
I've tried using the below config for the second node server...
location /node2/ {
proxy_pass http://192.168.1.100:3000/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
When I load the https://example.com/node2/ url, the html of the root page of the second node server is displayed but none of the css, js or images are loaded so the page doesn't look or work as it should and I see the following in the browser console...
Mixed Content: The page at 'https://example.com/node2/' was loaded
over HTTPS, but requested an insecure image
'http://192.168.1.100:3000/assets/graphics/logo.png'. This content
should also be served over HTTPS.
and
Failed to load resource: net::ERR_CONNECTION_CLOSED https://192.168.1.100:3000/assets/css/styles.min.css
for css and js assets... so it seems that no redirection is taking place for assets and also any links on the html page do not show the /node2/ suffix when hovered over or when clicked as the page tries to load the resource from https://example.com instead of https://example.com/node2/
Is it possible to actually do what I want in terms of reverse proxy two locations and can anyone point me in the right direction on how I can get this work as need?
The application being loaded is set up to load assets from the server root, not with server root + /node2/

Nginx infinite redirect loop when using multiple proxy_pass

I need some help with this one, because I've been stuck on it for far too long. Here's my use case:
I need to proxy from my main domain test.co to two separate locations, at various endpoints. For now, however, I simply want to use location blocks for = / (homepage) and / (everywhere else). The two separate locations are webflow.test.co at the homepage (real domain removed for anonymity), a subdomain of ours, and our NodeJS server at everywhere else. Here's the problem:
Using the code below, (my nginx.conf, ran through an ElasticBeanstalk instance) results in an infinite redirect loop at the homepage /. The error in the browser is ERR_TOO_MANY_REDIRECTS, but it's clear what's happening: the browser is redirecting from the original test.co to https://test.co, to https://test.co again ad nasueum. I've tried changing nearly everything I could think of... some examples: the server_name was originally a regex, I removed that. The / location block was once a regex, had some issues with that, I removed it. Added X-Forwarded-Proto $scheme to the headers.
Those are just a few things, but here's the bottom line: the only thing that seems to work, through 3 days of testing, is removing proxy_set_header Host $host;, or setting it to webflow.test.co (thus defeating the purpose). This is an unacceptable solution, of course, because I need the URL to match my original domain... but it does solve the problem. Perhaps that can give you guys some insight.
Here's my code:
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
# Redirect all non-HTTPS to non-WWW HTTPS
server {
listen 8080;
server_name "~^(?:www\.)?(.*)$";
return 301 https://$host$request_uri;
}
# Redirect WWW HTTP to non-WWW HTTP
server {
listen 4430;
server_name "~^www\.(.*)$";
return 301 https://$1$request_uri;
}
# Reverse-proxy to http://nodejs
server {
listen 4430;
server_name test.co;
client_max_body_size 50M;
location = / {
proxy_pass http://webflow.test.co;
proxy_set_header Connection "";
proxy_http_version 1.1;
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://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Thanks so much to anyone who takes the time to solve this one. It's been a thorn in my side, and I've gotta move on to something fresh. Will accept the first answer that works immediately.
Some other notes - may be important - that I'll add to if things get uncovered:
curl -v test.co returns a 301 with a Location of https://test.co.
curl -v https://test.co returns the same - 301 with a Location of
https://test.co
Removing the two non-WWW and non-HTTPS at the top doesn't solve the
problem. It makes test.co unreachable, but https://test.co still
redirect loops

bedrock wordpress, Nginx proxy & node.js

Problem: a server behind proxy unreachable for api listening port 3050 with bedrock wordpress.
I had deployed wordpress with Trellis to my VMs. Everything is up and running perfectly.
The wordpress site is accessible to the http://example.com but it won't able access through IP address like http://192.168.1.157
I had setup a node api that listening on port 3050. but when I try to browse/curl it with http://192.168.1.157:3050 or http://192.168.1.157/api
Chrome is return: This site can’t be reached ; ERR_CONNECTION_REFUSED
I only able to reach the site with http://example.com/api
I have an Nginx reverse proxy that helps me route to my Proxmox VMs.
Here are the routing rules written on my reverse proxy:
location ~* /api/ {
client_max_body_size 500M;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto http;
rewrite ^/api/(.*) /$1 break;
proxy_pass http://192.168.1.157:3050;
}
I am looking for a way, that my Trellis Wordpress can be accessed through http://192.168.1.157 . I think it will help me solve the problem.
Any advice is appreciated
Thanks

Forwarding port to Node.js app with Nginx and routing

I run my node app on localhost:3000 and it is serving a default page for the route /. If I access http://localhost:3000 the default page is displayed accordingly. I have also running a Nginx server that is basically configured as followed:
server {
listen 80;
server_name localhost;
location /node_app {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
If I run http://localhost/node_app now, my node app throws an error saying that it cannot find route /node_app.
How can I configure either my node app or the nginx server in a way that I can access the app by calling http://localhost/node_app, yet the app itself thinks it is at /?
Update
If I add a / to http://127.0.0.1:3000 it is actually matching /node_app to the / route. But now every stylesheet for instance within the default page is now pointing to the wrong path.
After experimenting a bit around I finally got the configuration right to work exactly how I wanted the server to work:
server {
location /node_app/ {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Lesson learned: Remember the slashes!

Resources