Reverse proxy in subdirectory can't find static assets - node.js

I'm using Nginx as a reverse proxy to route requests to localhost to my node server, but requests to localhost/blog to a ghost server (a blogging platform)
My Nginx config is like this:
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ^~ /blog {
rewrite /blog/(.*) /$1 break;
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:2368;
proxy_redirect off;
}
}
edit: The reason I'm rewriting the URL with rewrite /blog/(.*) /$1 break; is so that the ghost server doesn't get the request to /blog/, which it can't use
However because ghost, the blogging platform, is designed to be run in the root directory, all static assets fail because they should be going to localhost/blog/static/asset/path rather than localhost/static/asset/path:
Is there any simple solution to this problem? Or do I have to rewrite all the HTML for ghost?

Related

Nginx Minio static files 404 on a context path

I have 2 machines: One holds the Minio running in a Docker container on port 9001 and the other holds the Nginx. I want to access\serve Minio on a path prefix /media i.e. www.mydomain.com/media: I can see that proxy_pass is working fine but I'm getting 404 on static files:
I can see that the main page is getting loaded by checking the favicon and the page title.
Below is my nginx config file:
...
upstream minio {
ip_hash;
server <hostname\IP>:9001;
}
server {
server_name mydomain.com;
...
location /media {
rewrite ^/media(/.*)$ $1 break;
proxy_pass http://minio/;
client_max_body_size 0;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $http_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-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Forwarded-Proto $scheme;
chunked_transfer_encoding off;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_connect_timeout 300;
}
...
}
Any help would be highly appreciated. :)
Unfortunately it seems like this isn't possible and the MinIO team isn't interested in supporting it natively:
https://github.com/minio/minio-js/issues/737
This comment suggests some workarounds, however: https://github.com/minio/minio-js/issues/737#issuecomment-809373153
reverse proxy via nginx - rewrite prefix and added option proxy_set_header Host '127.0.0.1:9000'
use traefik with stripPrefix and sets static header Host

nginx doesn't redirect backend

I've nginx config that route frontend app to specific port, and backend to specific route
here's my configs
server {
listen 80;
server_name test.com www.test.com;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
location /api {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:3000;
proxy_set_header X-Real-IP $remote_addr;
}
}
when trying to access test.com directly it works, but when trying to access test.com/api , it doesn't work, then if return to main path test.com it also doesn't work, it seems like nginx stopped working after accessing the /api

Socket.io 404 on nginx

So I have been looking through a lot of posts, websites, and have still not gotten this problem fixed.
I have previously had a project running on my server, including socket.io, without problems.
But now that I am uploading this new project to the server, it seems that the socket.io always returns 404.
Sorry if I have overlooked something obvious here.
Nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /demo/ {
proxy_pass http://localhost:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
location /socket.io/ {
proxy_pass http://localhost:3000/;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_pass_request_headers on;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
My socket.io server:
const app = express();
const server = http.createServer(app);
const io = require('socket.io')(server);
server.listen(3000);
Client (Pug):
script(src="/socket.io/socket.io.js")
Note that the code above is what I have ended up with after reading through a lot of questions on here, on serverfault, aswell as blog posts several places.
I don't remember using a second location "tag" (is that what it is called?) for socket.io for the first one I used.
The proxy pass should be:
location /socket.io/ {
proxy_pass http://localhost:3000/socket.io/;
...
}
Please see https://medium.com/#ibraheemabukaff/how-to-proxy-websockets-with-nginx-e333a5f0c0bb for details...
Giving your Nginx the server name localhost and then trying to proxy_pass to localhost is probably not going to do you any favours.
Try getting rid of the server_name localhost; entirely and changing all these:
proxy_pass http://localhost:xxxx;
to this:
proxy_pass http://127.0.0.1:xxxx;
Or, if you want to do it properly then outside of your server block create an upstream directive:
upstream socketserver {
server 127.0.0.1:3000;
}
and change your proxy_pass directives to:
proxy_pass http://socketserver/;

nginx : redirect to port according to domain prefix (dynamically)

I'm trying to use nginx to redirect to ports (running nodeJS apps) based on the domain prefix. So far, I can redirect
example.com:80 --> port 8502
5555.example.com:80 --> port 5555
6666.example.com:80 --> port 6666
Is there a way to do this kind of redirection without having to copy-paste this over and over??
server {
listen 80;
server_name 5555.example.com;
location / {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_pass http://example.com:5555;
}
}
I figured I should do this with regular expressions, so I tried the following, but without any success :
~^(?<theport>.+)\.example\.com$ #then changed proxy_pass to http://example.com:$theport
~^([0-9]+)\.example\.com$ #then changed proxy_pass to http://example.com:$1
server_name "~^([0-9]{4})\.example\.com$";
set $theport $1; #then changed proxy_pass to http://example.com:$theport
In all cases, I'm getting a "502 Bad Gateway" error.
I found the solution! The regular expression works, but you need to add a resolver in order to have a variable in the proxy_pass (at least, that's how I understand it).
server {
listen 80;
server_name ~^(?<port_subdomain>[0-9]*).example.com$;
location / {
resolver 10.33.1.1; #/etc/resolv.conf
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_pass http://example.com:$port_subdomain;
}
}

Nginx setup for local webapp and websocket

following is my nginx configuration,
server { //PART-1
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:8090;
proxy_redirect off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
}
server { //PART-2
listen 80;
server_name service;
root /usr/local/tomcat7/webapps/service-snapshot;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/ServiceUI/;
}
}
first part of config works fine for websockets, which I am already using.
Second part of config is for webapp running on Apache tomcat 7.0.56, which is not working.
Is there something wrong with config? assuming server_name in both parts might be causing issue!
Any suggestions!
While having multiple services on one IP and port is working perfectly fine, the server_name directive is using the HOST header submitted by the client/browser. In this case, you're not supplying the header but instead asking for a specific location on the same server (you're not asking for http://_ or http://service but for http://yourserver/services from what I see in the comments).
To make it work, you have to specify the different services via locations like this:
server {
listen 80;
server_name THIS_IS_WHERE_YOUR_DOMAIN_OR_MAYBE_LOCALHOST_GOES;
location / {
proxy_pass http://127.0.0.1:8090;
proxy_redirect off;
proxy_pass_request_headers on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection Upgrade;
}
location /Service {
root /usr/local/tomcat7/webapps/service-snapshot;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080/ServiceUI/;
}
}

Resources