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

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;
}
}
}

Related

redirecting HTTP to HTTPS and proxy forwarding

So I'm trying to redirect port 80 to 443. I'm also trying to do a proxy forwarding to locahost:4000 with socket.io. are both correct or incorrect these are in nginx/sites-enabled/ap.kosherup. Not sure why the server { got cut off from rest of code. Also where would I add the security headers in the file? I am a noob.
server {
listen 80;
listen [::]:80;*
root /var/www/api.kosherup/html;
index index.html index.htm index.nginx-debian.html;
server_name api.kosherup www.api.kosherup;
return 301 https://api.kosherup.xyz$request_uri;
location /socket.io {
proxy_set_header Host $host;
proxy_pass http://localhost:4000/socket.io/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /v1 {
proxy_pass http://localhost:4000/v1;
}
}
server {
listen 443;
listen [::]:443;
root /var/www/api.kosherup/html;
index index.html index.htm index.nginx-debian.html;
server_name api.kosherup www.api.kosherup;
location /socket.io {
proxy_set_header Host $host;
proxy_pass http://localhost:4000/socket.io/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header Host $host;
}
location /v1 {
proxy_pass http://localhost:4000/v1;
}
}

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;
}

Nginx configuration - https works but not http

I have an issue with a freshly configured Nginx setup on Debian 9.
My site loads fine using https, but I get a 404 Not Found when I access it using http.
I tried removing the ssl certificate, it works however i need the location /webex/receive in https and /ping and /mailgun in http.
See my edited down server block:
server {
listen 80;
listen 443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
location /ping {
proxy_pass http://xx.xx.xx.xxx: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 /mailgun {
proxy_pass http://xx.xx.xx.xxx: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 /webex/receive {
proxy_pass http://xx.xx.xx.xxx: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;
}
}
All location (/ping, /mailgun and /webex/receive) work in https but I only want /webex/receive in https and the others locations /mailgun and /ping in http.
I found the solution
server {
listen 80;
listen [::]:80;
root /var/www/xx.xx.xx.xxx/html;
index index.html index.htm index.nginx-debian.html;
server_name xx.xx.xx.xxx www.xx.xx.xx.xxx;
location / {
try_files $uri $uri/ =404;
}
location /ping {
proxy_pass http://xx.xx.xx.xxx:3000;
}
location /mailgun {
proxy_pass http://xx.xx.xx.xxx:3000;
}
}
server {
listen 443 ssl;
include snippets/self-signed.conf;
include snippets/ssl-params.conf;
location /webex/receive {
proxy_pass http://xx.xx.xx.xxx:8080;
}
}

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;
}

nginx won't change root to sub directory

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;
}
}

Resources