Nginx configures the same domain name to support both http and https access

Nginx configures the same domain name to support both http and https access

Nginx is configured with the same domain name, which can be accessed by both http and https. The certificate is applied for free on Alibaba Cloud.

server
{
listen 80;
listen 443 ssl;
ssl on;
server_name domain name;
index index.html index.htm index.php default.html default.htm default.php;
ssl_certificate /usr/local/nginx/cert/21402058063066221.pem; //Download the pem provided by Alibaba ssh after application
ssl_certificate_key /usr/local/nginx/cert/21402058063066221.key; //Download the key provided by Alibaba ssh after application
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

 
root /home/wwwroot/website directory;

include laravel.conf; //Okay, here is the laravel configuration, which may not be suitable for you, please ignore it #error_page 404 /404.html;
include enable-php.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

access_log /home/wwwlogs/airclass.mime.org.cn.log;
}

The key lies in the listen 80 above;

listen 443 ssl; open port 80

Of course, there is no point in playing like this. Since it is https, there is no need to transmit data via http. We must forward all http requests to https.

The nginx redirect command is used to redirect http to https. So how should redirection be written? Older versions of nginx may have used a format similar to the following.
That is to add another virtual machine server, port 80

server {
listen 80;
server_name www.domain.com;
rewrite ^/(.*) https://$server_name$1 permanent; #Jump to Https
}

The rewrite still has different versions as follows

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

or

rewrite ^ https://domain.com$request_uri? permanent;

Now the new version of nginx has changed the writing method, and the above ones are no longer recommended. There are probably still many articles on the Internet that talk about the first type.

The following is the latest supported way to redirect nginx http pages to https pages:

server {
listen 80;
server_name domain.com;
return 301 https://$server_name$request_uri;
}

server {
listen 443 ssl;
server_name domain.com;

}

But my nginx/1.10.0 doesn't seem to work, maybe it doesn't support this way of writing...

The following is a complete configuration based on http to https:

server
{
#listen 80;
listen 443;
ssl on;
server_name domain.com; //your domain nameindex index.html index.htm index.php default.html default.htm default.php;
ssl_certificate /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.pem;
ssl_certificate_key /usr/local/nginx/cert/user.medsci-tech.com/214020580630662.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

root /home/wwwroot/web/public; //Project root directory include laravel.conf;
#error_page 404 /404.html;
include enable-php.conf;

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}

location ~ .*\.(js|css)?$
{
expires 12h;
}

}
server {
listen 80;
server_name domain.com;
rewrite ^/(.*) https://$server_name$request_uri? permanent;
}

This is the end of this article about configuring Nginx to support both http and https access on the same domain name. For more related content about Nginx supporting both http and https on the same domain name, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Example of how to configure nginx to implement SSL
  • Nginx implements https website configuration code example
  • Detailed tutorial on configuring nginx for https encrypted access
  • Implementation of Nginx domain name forwarding https access
  • Alibaba Cloud Nginx configures https to implement domain name access project (graphic tutorial)
  • Detailed explanation of the principle and implementation process of Nginx configuration https
  • Detailed configuration of Nginx supporting both Http and Https
  • Implementation of HTTP and HTTPS services with Nginx reverse proxy for multiple domain names
  • Example code for using Nginx to implement 301 redirect to https root domain name
  • How to change the website accessed by http to https in nginx

<<:  Detailed explanation of MySQL 8.0 dictionary table enhancement

>>:  Vue3 Documentation Quick Start

Recommend

Pure CSS to achieve hover image pop-out pop-up effect example code

Implementation principle The main graphics are co...

Solve the problem of the container showing Exited (0) after docker run

I made a Dockerfile for openresty on centos7 and ...

MySQL scheduled task implementation and usage examples

This article uses examples to illustrate the impl...

mysql 8.0.18 mgr installation and its switching function

1. System installation package yum -y install mak...

Summary of common MySQL commands

Set change mysqlroot password Enter the MySQL dat...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

MySQL's method of dealing with duplicate data (preventing and deleting)

Some MySQL tables may contain duplicate records. ...

MySQL Order By Multi-Field Sorting Rules Code Example

Say it in advance On a whim, I want to know what ...

How to use file writing to debug a Linux application

In Linux, everything is a file, so the Android sy...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

Example of writing mobile H5 to invoke APP (IOS, Android)

iOS 1. URL scheme This solution is basically for ...

Websocket+Vuex implements a real-time chat software

Table of contents Preface 1. The effect is as sho...

js to achieve interesting countdown effect

js interesting countdown case, for your reference...

IDEA graphic tutorial on configuring Tomcat server and publishing web projects

1. After creating the web project, you now need t...