Redirect HTTP to HTTPS / SSL with nginx.conf

Hello I’d like to change the nginx config for a website made in local with Local. I’ve now exported it to my server and I did the necessary to put the https. It works fine but it still works as well with http. how can I find the nginx.conf in my server config file to redirect the http to https?
When find this file , is the code below is the rigth one?

server {
listen 1.2.3.4:80 default;
server_name example.com www.example.com;
## Redirige le HTTP vers le HTTPS ##
return 301 https://$server_name$request_uri;
}

thank you

Hey @mokbelok,

Frist thing to notice about HTTPS in Wordpress is that if configured correctly it should have all the references and page paths using the https protocol. You can force the Wordpress back-end to always use HTTPS with the option:

define( 'FORCE_SSL_ADMIN' , true);
You’ll need to put it into the wp-config.php file

But there are still some configurations and changes to be made.
I’m pretty sure you looked into it before, but just to be safe, this is a *good* article explaining how to use HTTPS/SSL with Wordpress and the process of convertion:

About the nginx configuration it depends a little on your server, shared, vps or reserved host… But this is another article that can help you:

If you need any further help, I’ll be here!
Hope it’s useful :smiley:

Hey atempel,
Thank you for your answer and thoses good articles!:ok_hand:
I tried to insert the code in the wp-config.php file but it still doesn’t work… I mean both http and https work fine but I only want the https to work. And if I write my site’s name in the browser it’s not redirectly automaticaly to the https.
I’ve checked the mixed content in WP, no problem on this side.
So maybe it’s because I didn’t submit my http and https site to the google search console?

Nice!

I tried to insert the code in the wp-config.php file but it still doesn’t work… I mean both http and https work fine but I only want the https to work.

The code I provided works only for the yoursite.com/wp-admin area. Which is the Wordpress dashboard, with this option, Wordpress forces all access on the dashboard to use https (redirecting all requests), so it shouldn’t run on http even if you put http on the browser. But it doesn’t change the general access to your site (like someone accessing your home page).

So maybe it’s because I didn’t submit my http and https site to the google search console?

No, Search Console don’t change how your site is accessed, it only changes how it appears on Google. If your site can be accessed through HTTP and HTTPS, and you register the Search Console account with “http://yoursite.com”, Google will index the http version of it. But if you registered like “https://yoursite.com” Google will register the https version, simple as that. You can even have both (but its not advisable).

Even if you register with http, but your site redirects all requests to https, Google will probably index your site with https, because GoogleBot will be redirected to https as well…

Did you try to put the HTTPS redirects on nginx? The best code for that is provided in one of the articles I sent you. This is very important, since it will redirect all access to https automatically.

thanks again for all those informations!
No I didn’t try to put the https redirects on nginx. The thing is that I used nginx through Local by Flywheel. I never installed it by myself. I used “Find any file” to locate the nginx.conf which is in the subdirectory of the LocalbyFlywheel file(see the upload file). Is it in this one that I’ve to put the code? because now my site is not anymore in Local but in my server.
With apache I can access the .htaccess in wp-config both on my local or in the server but with nginx I don’t know how to manage it… I can’t find this file (nginx.conf) in my live site.
Sorry but I as a begginner I have a lot to learn!!

38|251x163

Ok, so your server runs on nginx right? I believe that is the reason you used it to make the site in Local.

To be able to find the nginx configuration file, if you have access to the command line on your server (SSH), see this link below, it shows the configuration path that nginx is using:

Generally, it resides in /etc/nginx/ but depending on the server it might be on a different location, and maybe you can’t even access it directly.

If you don’t have access to command line, do you have any type of control panel or dashboard from your host where you might be able to configure nginx?
There are only two ways to change those configs (that I know of), one is by directly editing it the nginx.conf file, and the other is by some kind of control panel or host provided server configurations.

ok I’m a little lost now… My serveur is o2switch, and yes I’ve a Cpanel with SSH access (I don’t know what is it, I’ve checked quickly and I have to generate a key…) So once I learn more about SSH and with the help of the link you sent me I can check the configuration path that nginx is using? and maybe reach the nginx.conf to put the code I need to to redirect all http request to https. is that correct?
Thx

Before you attempt SSH, which is an advanced tool, you can try putting a redirect through cPanel… This article from o2switch explains the interface further:

https://faq.o2switch.fr/hebergement-mutualise/tutoriels-cpanel/redirections

You will need to select your domain without https to be redirected to https://yourdomain.com and check the option “Wild Card Redirect” ( Redirection générique).
This should redirect all http access to https, even other files or paths.

If that doesn’t work you may proceed to try SSH and access nginx.conf to do the redirection… There’s also a documentation from o2switch about that:

https://faq.o2switch.fr/hebergement-mutualise/acces-ssh-shell-cli-cmdline

Hope it helps!

Hey atempel,
Good news, I have redirect it in the cpanel following the instruction, and it works! The cpanel redirection put a code in the .htaccess… that’s weird because I had not this file in my Local as I was working on nginx… So I think that when I uploaded my Local site to my server, it convert nginx into apache and create a .htaccess…
Anyway it works and I know how to do it know :slight_smile:
Thanks so much for your help and your well documented answers!

1 Like

haha Nice! Glad to know! Really!

Just so you understand, nginx is sometimes only a “layer” but apache is behind it. In your case, I believe your server have only apache and you built a site with nginx, this is not a huge difference but for testing, is better for you to reproduce exactly the environment of your host server.

Apache and Nginx in Local are just for you to reproduce your server, nothing that is set there is passed to the host, so the configurations you make on Local’s server won’t make any changes on the host server, the closest you can get of that is if you publish on the live site, the .htaccess created on Local, which is not advisable…

But I’m very happy you sorted the problem out, and maybe learned a thing or two on the process :smiley:

This topic was automatically closed 12 hours after the last reply. New replies are no longer allowed.