nginx won't change root to sub directory - nginx

I'm new to nginx and I'm trying to get my second.domain.com to display the contents of first.domain.com/dir, ( running on port 3000 of my localhost ) after looking online it seems this is the solution
# this one works fine
server {
listen 80;
server_name first.domain.com;
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_cache_bypass $http_upgrade;
}
}
# this one doesn't work as expected
server {
listen 80;
server_name second.domain.com;
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_cache_bypass $http_upgrade;
root /dir;
index index.html;
}
}
but when I visit second.domain.com I'm getting the same root as first.domain.com rather than first.domain.com/dir ...can anyone see what I'm doing wrong?

riffing off of mim's comment, I got it to work this way
# this one is now working as planned :)
server {
listen 80;
server_name second.domain.com;
location / {
# added mim's suggestiong here
rewrite /folder/(.*) /$1 break;
# then added the folder after the port
proxy_pass http://localhost:3000/folder;
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;
root /dir;
index index.html;
}
}

Related

cannot start Tomcat 9 with Nginx in ubuntu(22.04)

Cannot start Tomcat 9 with Nginx in ubuntu(22.04).
Here is my Nginx setting:
server {
listen [::]:80;
listen 80;
server_name 35.221.197.155;
location / {
proxy_pass http://0.0.0.0: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;
}
}
Any wrong setting here? Please tell me.

How can I properly configure nginx to work with Angular with using hot reload?

I run my server app with command: ng serve and get error in browser console:
Firefox cannot connect to the wss://dev.domain.com/ws server.
[webpack-dev-server] Trying to reconnect...
or on chrome:
WebSocket connection to 'wss://dev.domain.com/ws' failed
[webpack-dev-server] Disconnected!
[webpack-dev-server] Trying to reconnect...
My config nginx is:
location ^~ / {
proxy_pass http://localhost:4200;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
location ^~ /sockjs-node/ {
proxy_pass http://localhost:4200;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
location ^~ /ws/ {
proxy_pass http://localhost:4200;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
This should work, but it doesn't. my configuration is wrong?
That works for me for Angular 14 :
location /ng-cli-ws {
proxy_pass http://host-gateway:4200;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
If your app is using /ws, try to remove ending slash, else nginx responds with 301 to /ws/...
I second Vlad's answer, but I would like to elaborate on the solution.
I wanted my Angular application to be able to reload any changes at development time and be able to display them through HTTPS. So the following is what is working fine for me. It includes configuration managed by Certbot; and omits an additional default server block.
upstream frontend {
server 0.0.0.0:4200 fail_timeout=3s max_fails=3;
server 0.0.0.0:80 backup;
}
upstream wsfront {
ip_hash;
server 0.0.0.0:4200;
}
server {
server_name my.domain.net;
location ~ ^/ng-cli-w(s|s/)$ {
proxy_pass http://wsfront;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
location / {
proxy_pass http://frontend;
}
listen [::]:443 ssl;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/my.domain.net/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/my.domain.net/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
server {
if ($host = my.domain.net) {
return 301 https://$host$request_uri;
}
listen 80 ;
listen [::]:80 ;
server_name my.domain.net;
return 404;
}

How can I add a `else` in nginx config file?

I have below nginx config file. I'd like to add another route which maps everything else to http://kibana-entrypoint:5601/. How can I do that in nginx?
http {
server {
listen 8080;
server_name localhost;
location /es/ {
proxy_pass http://es-entrypoint:9200/;
}
location /health {
proxy_pass http://es-entrypoint:9200/_cluster/health;
}
location EVERYTHING_ELSE {
proxy_pass http://kibana-entrypoint:5601/;
proxy_redirect off;
proxy_buffering off;
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;
}
}
}

Nginx "ERR_TOO_MANY_REDIRECTS" when redirecting http-to-https + reverse proxy

I've configured nginx to forward HTTP to HTTPS. This works for everything except my reverse proxy websites (that are also configured in nginx). I am getting the error ERR_TOO_MANY_REDIRECTS in my web browser for reverse proxy sites. I'm not sure what I'm doing wrong.
Below, is the configuration I'm using in nginx.conf:
server {
listen 80;
server_name localhost;
return 301 https://$host$request_uri;
location / {
root "C:\inetpub\wwwroot";
index index.html index.htm;
}
location /files {
proxy_pass http://localhost:89;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /photos {
proxy_pass http://localhost:89/photos/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}

Nginx config for subdomain and forum

I'm trying to configure nginx to do the following:
redirect example.com and www.example.com to my old website
www.example.com/forum or example.com/forum to forum webserver (ip)
any other subdomain to .example.com, reverse proxied to node.js
I know the following does not work, how should I configure?
server {
listen 80;
server_name www.example.com example.com;
location /forum {
proxy_pass http://<forum ip>/;
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;
}
return 301 $scheme://www.old-website.com;
}
server {
listen 80;
server_name ~^(.*)\.example\.com $;
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_cache_bypass $http_upgrade;
}
}
The naked return 301 will prevent the location /forum block from being considered. Try wrapping it inside a default location block:
location /forum {
...
}
location / {
return 301 $scheme://www.old-website.com;
}

Resources