NGINX is a lightweight webserver with much more features than just being a webserver, but I'm just going to dig a bit deeper into the webserver functionality for now.
As the title says, this is about redirecting traffic from http to https using nginx. I'll be hosting the site on the nginx directly. This is how a simple config for serving static html might look like:
[archy@websrv ~]$ sudo cat /etc/nginx/sites-available/http_example.com.conf
server {
listen 80;
server_name www.example.com;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ = 404;
}
}
If you want to redirect traffic from http to https, you'll have to do just minor changes to the config:
[archy@websrv ~]$ sudo cat /etc/nginx/sites-available/http_example.com.conf
server {
listen 80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}
As you can see, the http-site is just set up to redirect every traffic to https.
Now let's go to the https-site config:
[archy@websrv ~]$ sudo cat /etc/nginx/sites-available/https_example.com.conf
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/ssl/certs/example.com.cert;
ssl_certificate_key /etc/ssl/private/example.com.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ = 404;
}
}
The root, index and location sections have been moved to the https-config since this is the declaration for the live site.
Enabling the new configs:
[archy@websrv ~]$ sudo ln -s /etc/nginx/sites-available/http_example.com.conf /etc/nginx/sites-enabled/http_example.com.conf
[archy@websrv ~]$ sudo ln -s /etc/nginx/sites-available/https_example.com.conf /etc/nginx/sites-enabled/https_example.com.conf
Feel free to comment and / or suggest a topic
Comments
Post a Comment