Home/Server & Website Managed Operations/CentOS 6 Configure Nginx to Automatically Redirect Port 80 to 443
Server & Website Managed Operations

CentOS 6 Configure Nginx to Automatically Redirect Port 80 to 443

1. Install Nginx via Yum

Configure the yum repository:
Create a new file named nginx.repo in /etc/yum.repos.d/ and add the following content:

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/6/$basearch/
gpgcheck=0
enabled=1

Install:

yum install nginx -y

2. Configure Port 80 to Redirect to 443 (Automatically redirect from HTTP to HTTPS)

Edit the /etc/nginx/conf.d/default.conf file and modify it as follows:
server {
listen 80;
server_name yunwei123.com;

rewrite ^(.*)$ https://$server_name$1 permanent;

access_log /var/log/nginx/host.http2https.access.log main;

}

3. HTTPS Reverse Proxy (HTTPS access is actually directed to the proxied port 9800)

server {
listen 443;
server_name yunwei123.com;

ssl on;
ssl_certificate /etc/pki/tls/certs/lee.crt;
ssl_certificate_key /etc/pki/tls/certs/lee.key;

access_log /var/log/nginx/host.note.access.log main;

location / {

proxy_pass http://127.0.0.1:9800;

resolver_timeout 30s;
proxy_set_header Host $http_host;
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;
proxy_redirect off;

}

}

FREE CHECKUP

Free server checkup

We will send a problem checklist and remediation advice within one business day — no obligation to engage.

135 410 16684
Weekdays Mon – Fri 10:00 – 22:00 (UTC+8) · 7×24 emergency response
Call now
CONTACT

Contact Us

How can we help?

* Required