Nginx redirect to Node.js backend - node.js

I want to redirect a URL e.g domain.com/api/ to a specific Node.js server, the root URL shows my website. At the moment I use this config:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3031/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
but it does not work.
What has gone wrong?
Thanks for help and best regards :)

You fogot closing bracket after location /api section.
Your config working at my machine.
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
server_name _;
location / {
try_files $uri $uri/ =404;
}
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3031/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
Also your nodejs backend must handle '/api' requests.

Please note that proxy_pass directive gets argument for backend and optional URI.
That means proxy_pass http://localhost:3031; will get the URI from the user, e.g /api/res.json so final URL for the NODE JS is: http://localhost:3031/api/res.json
But when you provide the URI to the directive itself, it overrides the requested URI in the matching location. e.g location /api and proxy_pass http://localhost:3031/; (note the suffixed slash). so the part /api is replaced by / and final URL is: http://localhost:3031/res.json.
from the NGINX docs:
If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive:
location /name/ {
proxy_pass http://127.0.0.1/remote/;
}
So it's important to understand that the part of the requested URI /name/ is replaced by /remote/ and then the rest of the requested URI is added to the final sent URI.

Related

How to keep Nginx upstream for ExpressJS routes?

I have an express app with two routes: an index route (/) and an article route (/article).
Now I try to deploy this application on an Ubuntu server behind a Nginx.
The problem I have is that the index route available at https://servername/app works fine, however, if I am entering a route (starting from index) via the upstream /app is lost and I am routed to https://servername/article/param which clearly is not available as I would like to be routed to https://servername/app/article/param.
It works smoothly on my local computer where no upstream/Nginx is interposed.
My Nginx config looks as follows:
upstream app {
server localhost:3000;
}
server {
#listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
# index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.test.at;
#Enable the verification for letsencrypt
location /.well-known {
root /usr/share/nginx/html;
}
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_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# try_files $uri $uri/ noen;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.test.at/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.test.at/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
location ~^\/(images|stylesheets|fonts|javascripts) {
expires 1M;
access_log off;
add_header Cache-Control "public";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET;
add_header Access-Control-Allow-Headers X-Requested-With,content-type;
add_header Access-Control-Allow-Credentials true;
root /var/www/webviews/app/public;
}
location ^~ /app {
#rewrite ^/noen(/.*)$ $1 break;
proxy_pass http://app/;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
server {
if ($host = example.test.at) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
server_name example.test.at;
return 404; # managed by Certbot
}
Do you see any issues in my Nginx config?
I missed a location section for the article, where the (.*) handles the param (subpath):
location ~^/article/(.*) {
proxy_pass http://noen;
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;
proxy_set_header X-Forwarded-Proto $scheme;
}

Vue and Node configuration nginx

I've hosted my first Vue and Node app but I have a problem. I want to load Vue files on diferent port so there is less stress on node. The problem is that with this current configuration I get this in browser: Cannot GET / even though when in Node router I add route with url / I get something. But I need to load this url from vue router not from express router. Why it loads from express ? This is my configuration file nginx:
server {
listen 80;
listen [::]:80 default_server;
return 301 https://$host$request_uri;
}
server {
# Enable HTTP/2
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name domain.com;
# Use the Letā€™s Encrypt certificates
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
# Include the SSL configuration from cipherli.st
include snippets/ssl-params.conf;
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:5000;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
location / {
root /var/www/html/Web/dist; // Vue dist folder
}
}
In your nginx config you need to add try_files $uri $uri/ /index.html; to you / location like so. This sends everything to your index.html file.
location / {
root /var/www/html/Web/dist; // Vue dist folder
try_files $uri $uri/ /index.html;
}

How to redirect url with proxypass nginx

I use nginx Nginx as web server, and project build use Nodejs. There are 2 Project, one run on port 6000 and another on port 7000.
Every request api.mysite.com will redirect to project 1 (localhost:6000), and request api.mysite.com/v2 will redirect to project 2 (localhost:7000).
But,I want request api.mysite.com/oauth/token redirect to localhost:7000/oauth/token
In the server block , I have configure like this
upstream mysiteapiv1 {
server localhost:6000;
}
upstream mysiteapiv2{
server localhost:7000;
}
server {
listen 80;
server_name api.mysite.com;
root /var/www/html/v1;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://mysiteapiv1;
}
location /v2 {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://mysiteapiv2;
}
location /oauth/token {
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header Host $http_host;
proxy_pass http://localhost:7000/oauth/token;
}
location /public/ {
try_files $uri $uri/ =404;
}
}
But request api.mysite.com/oauth/ resulted not found
What should I do ?

nginx reverse proxy for nodejs express,works with ip address but not domain name

I have nginx works as a reverse proxy for a node.js express application.
I can access to
http://ip:8081/data/emoji.json
but it doesn't work with domain name
http://domain_name/data/emoji.json
doesn't work.
Here is the configuration of nginx:
server {
listen 80 default_server;
listen [::]:80 default_server;
index index.html index.htm index.nginx-debian.html;
server_name domain_name;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
proxy_pass http://127.0.0.1:8081;
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 /mongo {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
rewrite /mongo/(.+) /$1 break;
proxy_pass http://127.0.0.1:28017;
break;
}
}
}

Nginx forward /socket.io requests to proxied nodejs server

I am trying to forward all /socket.io requests to /broadcaster. Here is my nginx config:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
access_log /home/ubuntu/logs/broadcaster/access.log;
error_log /home/ubuntu/logs/broadcaster/error.log;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
# Enable WS
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
location /broadcaster {
rewrite ^/broadcaster/(.*) /$1 break;
proxy_pass http://127.0.0.1:1337;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# simply forward socket.io requests.
# only one site can run socket.io on this server now
location /socket.io {
proxy_pass http://127.0.0.1:1337;
# proxy_set_header Upgrade $http_upgrade;
# proxy_set_header Connection $connection_upgrade;
}
}
When I (socket.io) makes a request like GET http://my-ec2-public-dns/socket.io/1/?t=1408981904508 I get the error 400 (Bad Request).
How can I nginx forward the /socket.io requests?

Resources