Redirect http to https and WWW to non-WWW NGINX Yii2 WordPress

Author: Rafal Marguzewicz
Published:
Categories: NGINXYii2
Tags:


nginx folderFull example redirect all HTTP requests to HTTPS and WWW to non-WWW with Nginx. You can use this for to WordPress, Yii2 basic application template.

SEO like redirect http to https and WWW to non-WWW (minimalism one [sub]domain).

Configuration in default.conf file

Copy fallowing code (ctrl + C):


server {
server_name domain.com www.domain.com;
        listen 80;
        return 301 https://$server_name$request_uri;
}
server {
server_name domain.com;
        root /home/path/to/site_folder/;
        include php.conf;
        include ssl.conf;
        ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
}
server{
server_name www.domain.com;
        include ssl.conf;
        return 301 https://domain.com$request_uri;
        ssl_certificate /etc/letsencrypt/live/www.domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/www.domain.com/privkey.pem;
}

and

sudo nano /etc/nginx/default.conf

Paste: ctrl + shift + V
Save: ctrl + X

Configuration in php.conf

Copy fallowing code (ctrl + C):

index index.php;

location / {
        try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
        fastcgi_buffers 8 16k;
        fastcgi_buffer_size 32k;
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
}
sudo nano /etc/nginx/php.conf

Paste: ctrl + shift + V
Save: ctrl + X

Configuration in ssl.conf

Copy fallowing code (ctrl + C):


listen 443 ssl http2;
listen [::]:443 ssl http2;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AES$
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;
}
sudo nano /etc/nginx/ssl.conf

Paste: ctrl + V
Save: ctrl + X

Redirect http to https and WWW to non-WWW NGINX Yii2 WordPress
4 / 4 votes


Your email address will not be published. Required fields are marked *

Name *
Email *

Read previous post:
Skype install problem api-ms-win-crt-runtime-l1-1-0.dll

Uninstall skype and download version 7.32:

Close