Nginx proxy_pass and absolute paths - node.js

I'm trying to run some nodejs app on a server (Ubuntu 14.04), using Nginx and i'm almost done. Here's my server configuration (/etc/nginx/sites-available/default):
server {
listen 80;
server_name my_domain.com;
location /test1 {
proxy_pass http://127.0.0.1:5000/;
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 /test2 {
proxy_pass http://127.0.0.1:5001/;
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;
}
}
I've got several apps running, and they all works well, i can acces them with http://my_domain.com/test1, http://my_domain.com/test2, etc...
The problem is that inside one of this apps i've got several absolute paths:
e.g. Home
or (inside express)
res.redirect('/');
This redirects don't go to http://my_domain.com/test1 but they go to http://my_domain.com/
Is there a way, through nginx configurations, to tell the app that the root location is actually http://my_domain.com/test1?
I'm really new to nginx and to virtual hosts in general, i'm trying to learn... Any help would be appreciated.
EDIT:
The result of curl -I http://127.0.0.1:5000 is:
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 1376
ETag: W/"560-GGm/YYltkhKxiOVNZ99jqQ"
set-cookie: connect.sid=s%3AkZYCqLKvbhHhK3W8ECBN8G91s41paws4.ekLWefrd3NdQatT3VzFNozfnFs65YBEW9k9MNTdbQT0; Path=/; HttpOnly
Date: Sat, 15 Aug 2015 13:13:20 GMT
Connection: keep-alive
As you can see i don't get a Location Header...
By the way, i managed to solve the problem using subdomains, that seem to work as i expected... Anyway an answer would be appreciated, since i might need it in the future.

Use curl to check the header Location value which is used by client to redirect. e.g. curl -v http://127.0.0.1:5001/myappp
Use proxy_redirect to change the Location header

Related

how can I deploy my mern website from my local machine?

I have a fully functioning MERN app working. I was wondering, how would I deploy my mern app to the world wide web from my local machine?
My research has concluded with XAMPP as a possible tool, but it seems the backend tooling is PHP not nodejs. Any ideas you guys have helps!
the idea is to host it locally not externally on the cloud.
Firstly, you need to buy vps server. After you need to upload full project to the server through ftp software and need to upload packages with npm install/update. After these are finished download nginx server to the server and be carefull about port of project. If your project will running for example on 3000 port you may change nginx configuration like this and restart nginx with followed command:
systemctl restart nginx
and nginx configuration write like this (added example domain):
server {
listen 80;
listen [::]:80;
server_name yourdomain.com;
root /var/projects/your_project_name;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html;
charset utf-8;
location / {
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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
}
For additional information about nginx configurations you may visit Nginx configurations

Nginx reverse proxy mapping does not redirect

I recently started tinkering to solve this problem I was having this problem. So I installed NGINX and set it up so that it forwards the incoming requests on port 80 to port 300 by creating a .conf file in /etc/nginx/conf.d/ location. Here is the configuration file.
server {
listen 80;
server_name xyz.xyz;
location / {
proxy_pass http://127.0.0.1:300;
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;
}
}
But when I hit xyz.xyz I get the default page and when I go to xyz.xyz:300 I get my NodeJS app. Any suggestion?
Seems like it was an issue with SELinux.
I just ran this command and it worked.
setsebool -P httpd_can_network_connect 1

Server Side Rendering with Nginx outputs HTML strings

I have a vue-ssr project, not using nuxt.js. And the server is express.
Project ran at port 3000.
When not using nginx, visited ip:3000, the page worked well.
When using nginx, visited my domain, I still could get HTML strings, but the strings were not rendered, like that:
(I don't have enought reputation to post images.)
html strings.png
And the request and response headers like that:
request and response headers.png
Here is my nginx config:
server {
server_name mydomain;
root /my/path;
location / {
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_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass http://localhost:3000;
proxy_redirect off;
}
}
I've tried the config introduced in nuxt.js documents, but didn't worked either.
If removing the nosniff header fixes the problem, that would be an indication that for some reason express is not sending the right mime types with your responses.
You could check that by hitting express directly and examining the headers it is returning, e.g.:
curl -D - -o /dev/null http://localhost:3000
If it's NOT returning text/html, it could be a sign you're accidentally breaking the default mime type somewhere in your application code.

HTTPS with Nginx and Nodejs

So I used certbot to configure Nginx for https. That worked well (I used the automatic configuration).
Now how can I configure my Node.js back end to be able to make GET/POST requests from the front end that is being hosted with Nginx?
EDIT:
location /api {
proxy_pass http://localhost:3000; #server port
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 / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
If you have correctly configured your Nginx with SSl and your node application. The requests you send should work with https://URL . Please check your nginx.conf and verify the following things.
You can add ssl cert in Nginx with
ssl_certificate /etc/nginx/ssl/server.crt #cert location
ssl_certificate_key /etc/nginx/ssl/server.key #key location
in your server block in Nginx.And your nodejs App should be configured like this in the server block
location / {
proxy_pass http://localhost:3000; #server port
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;
}
This will be sufficient for your server to work with SSL.

MEAN stack deployment on server

I create AWS instance and installed Nginx server for my project. Now for angular I create ang.conf and for node create node.conf file in site-available. Share my conf file
ang.conf
server {
listen 80;
server_name IP;
location / {
root project_path;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 403 /403.html;
# To allow POST on static pages
error_page 405 =200 $uri;
}
node.conf
server {
listen 3004;
server_name IP;
location / {
proxy_pass http://IP: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;
}
}
My node server working fine. I can use my api through postman with the port for example http://MY_YP:3000. But in angular site when I go to browser and go login page and click on submit button not connect to node js server. When I check my response in network it return like this.
Response
HTTP/1.1 200 OK
ETag: W/"5b486d9c-848"
Content-Type: text/html
Date: Fri, 13 Jul 2018 09:56:38 GMT
Last-Modified: Fri, 13 Jul 2018 09:15:08 GMT
Server: nginx/1.10.3 (Ubuntu)
Connection: keep-alive
Content-Encoding: gzip
Transfer-Encoding: Identity
I don't what's wrong in this code. Please suggest me how to handle this.
Finally got the answer. I have to change my nginx.conf file.
events {
worker_connections 4096; ## Default: 1024
}
http {
# Change this depending on environment
upstream api {
server 192.168.0.1:9998;
#put here node.js ip with port
}
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
location / {
# If you want to enable html5Mode(true) in your angularjs app for pretty URL
# then all request for your angularJS app will be through index.html
try_files $uri /index.html;
}
# /api will server your proxied API that is running on same machine different port
# or another machine. So you can protect your API endpoint not get hit by public directly
location /api {
proxy_pass http://api;
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_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#Static File Caching. All static files with the following extension will be cached for 1 day
location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
expires 1d;
}
}
}

Resources