Operating System & Version
almalinx 8
cPanel & WHM Version
110.0.7

Hefin

Active Member
Feb 23, 2021
25
6
3
London
cPanel Access Level
Website Owner
I'm trying to figure out how to force a WordPress site running on NGINX in cPanel to only use SSL. I updated the sites nginx conf file with the following line shown in bold below:


server {
server_name mysite.com www.mysite.com mail.mysite.com;
listen 80;
listen [::]:80;

return 301 https://mysite.com$request_uri;

include conf.d/includes-optional/cloudflare.conf;

set $CPANEL_APACHE_PROXY_PASS $scheme://apache_backend_${scheme}_11_222_333_44;


I restarted NGINX but the site fell over with a message saying too many redirects
 

Hefin

Active Member
Feb 23, 2021
25
6
3
London
cPanel Access Level
Website Owner
I don't like using plugins where I don't really need them. When I used apache for the webserver I could just edit the .htaccess file, but obviously NGINX doesn't use that file.
 

ffeingol

Well-Known Member
PartnerNOC
Nov 9, 2001
963
437
363
cPanel Access Level
DataCenter Provider
You can certainly do it only with the web server, as long as the URL's for the site (in WordPress settings) are set to https and not http. If they are, you'll see the loops like you are currently seeing.
 
  • Like
Reactions: Hefin

Hefin

Active Member
Feb 23, 2021
25
6
3
London
cPanel Access Level
Website Owner
You can certainly do it only with the web server, as long as the URL's for the site (in WordPress settings) are set to https and not http. If they are, you'll see the loops like you are currently seeing.
Yeah, that would make sense, but in practice, it didn't make a difference. I updated the general settings to include HTTPS as the protocol prefix (not the real site name) and made the necessary changes to the Nginx configuration again. However, I encountered the same issue again with the browser complaining about too many redirects.

1687209669394.png


Strange thing is, though, that now it's set in the WordPress general settings tab, the site effectively behaves like it's got HTTPS forced, as you can't load the HTTP version of the site.

It would be nice to know why the config update didn't work, though. Your explanation does make sense, but it's not what I see on the site. Either way, the end result has been achieved.
 
  • Like
Reactions: ffeingol

Hefin

Active Member
Feb 23, 2021
25
6
3
London
cPanel Access Level
Website Owner
For anyone else bumping up against this issue, I found this worked in the end

# Force https
if ($scheme = http) {
return 301 https://$host$request_uri;
}