WordPress HTTPS with NGINX
1. Introduction
- Advantage of having a https Proxy is to be able to read the URL for virtual Http Hosts.
- Problem of WordPress is that it has http links internal and won’t rewrite them just by giving a https Site URL.
- Rewrite Rule in “sites-available” or .htaccess rewrite does not work.
- It has to be rewritten in the php-config in wordpress itself
- webaccess will end up into loops
This page isn’t working
webserver.net redirected you too many times.
Try clearing your cookies
ERR_TOO_MANY_REDIRECTS
2. Prepare certbot
- I asume you have a valid certificate on your SSL Proxy with Certbot
sudo apt install certbot
sudo apt-get install python-certbot-nginx
sudo certbot --nginx -d webserver.net -d www.webserver.net
sudo certbot renew
3. Remove Url Rewrite Directions
First remove all rewrite Rules in Sites-available (/etc/nginx/sites-available/default or /etc/apache2/sites-available/default) and in the .htaccess file in the html Directory (/var/www/html/wordpress/.htaccess)
4. Configure SSL Proxy
On the SSL Server write the following in to the file /etc/nginx/sites-available/default
server {
index index.html index.htm index.nginx-debian.html;
server_name www.webserver.net webserver.net; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/webserver.net/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/webserver.net/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
location / {
proxy_pass http://192.168.122.10:80/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https; # important!!!
proxy_set_header X-Forwarded-Port 443;
}
}
server {
if ($host = www.webserver.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = webserver.net) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 ;
listen [::]:80 ;
server_name www.webserver.net webserver.net;
return 404; # managed by Certbot
}
5. Configure Webserver
On the Webserver create your Webservices where each Service is located on a different Port number, so you have a clear distinction of the Webservice area. (this is for apache2 but should be similare for nginx)
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/wordpress
<Directory /var/www/html/wordpress/>
Options +FollowSymlinks
AllowOverride All
Require all granted
SetEnv HOME /var/www/html/wordpress
SetEnv HTTP_HOME /var/www/html/wordpress
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
6. Configure WordPress
Now add the following to the /var/www/html/wordpress/wp-config.php after the database entries.
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
$_SERVER['HTTPS'] = 'on';
$_SERVER['SERVER_PORT'] = 443;
}
7. Multible webpages
If you want more than one webservice then make a copy of section 4. and 5. within the files.
- adapt the content for a different port like Port:81.
- Also adapt the hostname for your new webservice like next.webserver.net.
- Don’t forget to extend your certificate in section 2.
- Now you heave clear distinction of your webservices, virtualhost , ….