I have 2 NodeJS applications running locally on port 3000 and 3001. I'm trying to route to them individually by using my_ip/tilbudsfinneren for one of them, and my_ip/hms for the other one.
My sites-available file looks like this:
server {
listen 80;
listen [::]:80;
root /var/www/my_ip/html;
index index.html index.htm index.nginx-debian.html;
server_name my_ip www.my_ip;
location /tilbudsfinneren {
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_cache_bypass $http_upgrade;
}
location /hms {
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;
}
}
Whenever I try to reach my_ip/hms or my_ip/hms/ping it does not reach the NodeJS application, I get get "Cannot GET /hms". Is there an issue with my nginx configuration, or is there maybe something else wrong?
If you want to serve both apps as reverse proxy you must use rewrite methods in both locations.
server {
listen 80;
index index.html index.htm index.nginx-debian.html;
location /tilbudsfinneren/ {
rewrite ^/tilbudsfinneren/(.*)$ /$1 break;
proxy_pass http://localhost:3000;
#other config...
}
location /hms/ {
rewrite ^/hms/(.*)$ /$1 break;
proxy_pass http://localhost:3001;
}
}
Related
/etc/nginx/nginx.conf file
in http section, given the below informaiton.
include /etc/nginx/conf.d/*.conf;
/etc/nginx/conf.d/myapp.conf file content
server {
listen 80;
listen [::]:80;
server_name _;
index index.html index.htm;
location / {
proxy_pass 'http://XXXXX:3005/';
}
location /admin/ {
proxy_pass 'http://XXXX:3000/';
}
}
http://XXXX is redirecting to the main page.
http://xxxxx/admin/ is not redirecting. Instead it is throwing below errors in chrome console.
error image
The same site is working fine http://xxxx:3000 it is working fine. No issues.
Whatelse am I missing?
Note: OS is Amazon Linux & I have tested the same configuration with sample help world node js program & it is working fine.
Make sure that your routing is correct and that you have restarted nginx after configuration.
Try the following configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3005;
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 /admin {
proxy_pass http://APP_PRIVATE_IP_ADDRESS: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;
}
}
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;
}
}
}
I have two node.js apps: one is my site and second is CI tool. I have the following nginx config:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8081/;
}
location /ci {
rewrite ^/ci(.*) /$1 break;
proxy_pass http://127.0.0.1:3000/;
}
}
When i open example.com in browser it loads all resources requested by index.html correctly, but when i open example.com/ci it loads only index.html and i got errors like these:
GET http://example.com/styles/styles.css 404
GET http://example.com/scripts/app.js 404
I have added <base href="/ci"> into head of index.html in my CI app, but it doesn't help.
I ended up with next nginx config:
server {
listen 80;
server_name example.com;
location / {
if ($http_referer ~* example.com/ci) {
rewrite (.*) http://example.com/ci$1;
}
access_log off;
proxy_pass http://127.0.0.1:8081/;
proxy_set_header Host $host;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location = /ci { rewrite ^ /ci/ last; }
location /ci/ {
access_log off;
proxy_pass http://127.0.0.1:3000/;
proxy_set_header Host $host;
proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Also on the client side of my CI app i specified domain and port for socket.io connection: io.connect('http://example.com:3000').
No <base> html tags needed.
I'd like to run multiple node app throught a Nginx server for the reverse proxy. I actually have something like that in my config file :
server {
listen 80;
server_name http://my.server.url;
location / {
root /absolute/path/to/index/;
index index.html;
}
location /foo/ {
proxy_pass http://127.0.0.1:3000; # here is an nodejs app runing
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;
}
}
The problem : In my nodejs app, I receive url with the '/foo/' before.
I try to monkey arround with rewrite but honestly I'm confuse, so I'd like to get '/bar/' instead of '/foo/bar/', how should i proceed?
What you need is called rewrite in nginx
location /foo/ {
rewrite ^/foo(.*)$ /$1 last;
proxy_pass http://127.0.0.1:3000; # here is an nodejs app runing
}
I am planning to run 2 node apps with express under one sub domain, with nginx serving the static files. In both node apps I use:
app.use(express.static(path.join(__dirname)));
I have the following nginx config:
server {
listen 80;
server_name sub.domain.com;
index index.html;
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 / {
proxy_pass http://localhost:232;
}
location ~ ^/(dist/|img/|app/|css/) {
root /var/www/app/main/;
}
location /admin {
proxy_pass http://localhost:233;
rewrite ^/admin /$1 break;
}
location ~ ^/admin/(dist/|img/|app/|css/) {
root /var/www/app/admin/;
access_log off;
expires max;
}
}
With this setup everything is great for the 'main' app (accessed at sub.domain.com), but for the 'admin' app (sub.domain.com/admin) the same static files are served as for the 'main' app. How should I modify my setup to achieve the correct behavior?
Check using your dev-tools to make sure that the static assets are attempting to be loaded from the correct URL; that is, http://sub.domain.com/admin/css/ or /admin/css/ etc. It is possible that the paths are being generated with respect to their projects root and appear as /css/, so when the browser tries to retrieve them, it goes to http://sub.domain.com/css/.
I think the order of location blocks in your configuration may be the issue. For more detailed info, check out this link. Try using this refactored configuration -
server {
listen 80;
server_name sub.domain.com;
index index.html;
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 ~ ^/admin/(dist/|img/|app/|css/) {
root /var/www/app/admin/;
access_log off;
expires max;
}
location /admin {
proxy_pass http://localhost:233;
rewrite ^/admin /$1 break;
}
location ~ ^/(dist/|img/|app/|css/) {
root /var/www/app/main/;
}
location / {
proxy_pass http://localhost:232;
}
}