Reputation: 61
I've been trying to forward traffic from "http://example.com/...", "http://www.example.com/..." and "https://example.com/..." to "https://www.example.com/..." on my nginx server.
I'm running WordPress software with default site url "https://www.example.com/" and also using permanent links.
My current nginx configuration file looks as like as the following one: http://pastebin.com/MxHUNtCc
Did try implementing these forwarding rules into nginx but I have no idea how to support WordPress permanent urls.
Thanks for reading.
Chris
Upvotes: 0
Views: 106
Reputation: 12795
You need three server blocks in total
The examples below are modified from your pastebin
#
# Server Block 1: Port 80 Redirection.
#
server {
listen 80;
server_name example.com www.example.com;
# Note that using "rewrite ^(.*)" is not optimal.
# See "pitfalls" link you already have at the top of your conf file.
return 301 https://$host$request_uri;
}
#
# Server Block 2: Port 443 Redirection from https://example.com.
#
server {
listen 443 ssl spdy;
listen [::]:443 ssl spdy;
server_name example.com;
ssl on;
ssl_certificate /etc/nginx/SSL/public.pem;
ssl_certificate_key /etc/nginx/SSL/private.pem;
return 301 https://www.example.com$request_uri;
}
#
# Server Block 3: Port 443 Processing for https://www.example.com.
#
server {
listen 443 ssl spdy;
listen [::]:443 ssl spdy;
server_name www.example.com;
ssl on;
ssl_certificate /etc/nginx/SSL/public.pem;
ssl_certificate_key /etc/nginx/SSL/private.pem;
# Rest of your conf file follows
HTTP Strict Transport Security
requirements.Upvotes: 0
Reputation: 11
Also you can play with server_name
directive and $host
to set redirection to specific scheme. If you want to redirect only the WWW subdomain then use following code.
server {
listen 80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}
I hope it works for you.
Upvotes: 1
Reputation: 1
You can impliment a permanent redirection of http url to https using following new server block ...
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
... and make sure existing server block is properly setup with 443 port and certificates.
It should work fine as many of users followed the same with WordPress as suggested here: https://atulhost.com/secure-nginx-letsencrypt-ubuntu
Upvotes: 0