So i’m trying to help someone figure out why their NGINX server keeps serving people the root document. We’ve tried several versions of the server config file, which is currently as listed below.
The expected behavior, from my understanding of the NGINX config setup, should be:
- If the address has a subdomain, 404 it.
- If the address is an HTTP connection, repoint it to HTTPS.
- If the URL contains the /panel, panel1, or panel2 directories, redirect them to the appropriate port.
- Default, serve from primary root.
server {
listen 80;
listen [::]:80;
listen 443;
listen [::]:443;
server_name ~^.+(?<!www\.)nameredacted\.com$;
error_page 404 https://nameredacted.com/404.html;
return 404;
}
server {
listen 443 default_server;
listen [::]:443 default_server;
ssl on;
ssl_certificate /etc/nginx/ssl/nameredacted.crt;
ssl_certificate_key /etc/nginx/ssl/nameredacted.key;
root /home/nginx/www/html/nameredacted;
error_page 404 https://nameredacted.com/404.html;
index index.html;
server_name .nameredacted.com www.nameredacted.com;
location / {
try_files $uri $uri/ =404;
}
location /panel {
rewrite ^/panel$ https://www.nameredacted.com:8080 redirect;
}
location /panel1 {
rewrite ^/panel1$ https://www.nameredacted.com:8081 redirect;
}
location /panel2 {
rewrite ^/panel2$ https://www.nameredacted.com:8082 redirect;
}
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name .nameredacted.com www.nameredacted.com;
rewrite ^ https://$host$request_uri? permanent;
}
2,3, and 4 all work. #1 is giving us grief. Am I missing something obvious?