403 forbidden for nginx locations - nginx

I have an nginx sites-enabled/default configuration:
server {
listen 80;
server_name localhost;
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;
}
location ~ /anotherroute {
alias /home/anotherroute/public;
index index.html;
}
}
When I access http://myipaddress/anotherroute it works fine but when I do http://myipaddress/anotherroute/yetanotherroute it comes back as 403 forbidden.
How can I fix this?

Possibly your folder yetanotherroute is existed, make sure you have files index.php or index.html already
Otherwise you should specified a file name such as: http://myipaddress/anotherroute/yetanotherroute/somefile.php

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

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 subfolder as location

My nginx.conf contains this:
server {
listen 80;
server_name myserver.com X.X.X.X;
root /var/www/html;
location / {
proxy_pass http://127.0.0.1:8082;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_buffering off;
proxy_connect_timeout 43200000;
proxy_send_timeout 43200000;
proxy_read_timeout 43200000;
proxy_redirect off;
proxy_set_header Proxy "";
}
location /api/socket {
proxy_pass http://localhost:8082/api/socket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /newapp {
#Should this be empty?
}
}
As you can see, both / and /api/socket point to a proxy. Both of them work.
Originally this line didn't exist:
root /var/www/html;
I added it. I also added the "location /newapp". What I want is to go to the index file under the folder /var/www/html when I type
myserver.com/newapp
However, I get a 404. Am I missing something in the config file?
If you would like the URI /newapp (or /newapp/index.html) to find the file /var/www/html/index.html, you will need to use the alias directive and not the root directive.
For example:
location /newapp {
alias /var/www/html;
}
See this document for more.
Creating the folder /newapp inside /var/www/html solves the issue. But I am not sure if this is the way to go. However, it worked then.

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 multiple domain configuration failed

502 bad gateway error, hmm I've no problem with my dns. I have a folder named 'app' and 'website' in my /home directory.
server {
listen 80;
server_name dashboard.abc.com;
location / {
root app;
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;
}
}
server {
listen 80;
server_name abc.com
location / {
root site;
index index.html index.htm;
}
}
Everything make sense here, not sure which part is causing the problem

Resources