This has to be a duplicate, but I have been searching for a long time and have not found anything.
When I type in the address of my website using http, I get the NginX default page (https works fine):
http://svija.love
The NginX config file contains, at the end:
server {
if ($host = svija.love) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name svija.love;
listen 80;
return 404; # managed by Certbot
}
This was added automatically by Certbot.
I would expect that the statement if ($host = svija.love) would catch the http request and redirect to HTTPS.
But it is not working that way.
Not being expert, it seems to me that the second bit, beginning with server_name svija.love, is in direct contradiction with the first part:
- the first block redirects if the host is svija.love
- the second block returns 404 if the host is svija.love
The actual, configured server name is live.svija.love, if that makes a difference.
Any clarification would be greatly appreciated.
[UPDATE] I removed the NginX default config file and HTTP now redirects to HTTPS as expected.
Still, if anyone can explain the two config blocks above I would love to better understand what they're doing.
[UPDATE] This was not a good solution (see below).
[UPDATE Here is the config given by nginx -T:
# configuration file /etc/nginx/nginx.conf:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile off;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
# configuration file /etc/nginx/modules-enabled/50-mod-http-image-filter.conf:
load_module modules/ngx_http_image_filter_module.so;
# configuration file /etc/nginx/modules-enabled/50-mod-http-xslt-filter.conf:
load_module modules/ngx_http_xslt_filter_module.so;
# configuration file /etc/nginx/modules-enabled/50-mod-mail.conf:
load_module modules/ngx_mail_module.so;
# configuration file /etc/nginx/modules-enabled/50-mod-stream.conf:
load_module modules/ngx_stream_module.so;
# configuration file /etc/nginx/mime.types:
server {
# must match domain name or IP address
# or else the default Nginx page will be shown
server_name antretoise.svija.site;
# directory of site's static elements
location /static/ {
root /home/antretoise;
}
access_log /opt/logs/access.antretoise;
error_log /opt/logs/error.antretoise error;
# pass all additional queries to our application
location / {
# parameters from /etc/nginx/uwsgi_params
include uwsgi_params;
# pass the traffic to the socket
# that the uWSGI server sets up
# SOCKETS MUST MATCH IN:
# /etc/uwsgi/sites/antretoise.ini
uwsgi_pass unix:/run/uwsgi/antretoise.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/antretoise.svija.site/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/antretoise.svija.site/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = antretoise.svija.site) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name antretoise.svija.site;
return 404; # managed by Certbot
}
# configuration file /etc/nginx/uwsgi_params:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param REQUEST_SCHEME $scheme;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
ssl_session_cache shared:le_nginx_SSL:10m;
ssl_session_timeout 1440m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers off;
ssl_ciphers "EC-AES128-SHA";
#———————————————————————————————————————— default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
#———————————————————————————————————————— svija.love
server {
server_name svija.love;
# directory of site's static elements
location /static/ {
root /home/svijalove;
}
access_log /opt/logs/access.svijalove;
error_log /opt/logs/error.svijalove error;
# pass all additional queries to our application
location / {
# parameters from /etc/nginx/uwsgi_params
include uwsgi_params;
# pass the traffic to the socket
# that the uWSGI server sets up
# SOCKETS MUST MATCH IN:
# /etc/uwsgi/sites/svijalove.ini
uwsgi_pass unix:/run/uwsgi/svijalove.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/svija.love/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/svija.love/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = svija.love) {
return 301 https://$host$request_uri;
} # managed by Certbot
server_name svija.love;
listen 80;
return 404; # managed by Certbot
}
# 6 other sites at end, all configured the same way
# except that in the last two lines,
# listen 80; is sometimes listed BEFORE return 404;