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

Vue page monitoring user preview time function implementation code

A recent business involves such a requirement tha...

How to delete garbled or special character files in Linux

Due to encoding reasons, garbled characters will ...

How to use VirtualBox to build a local virtual machine environment on Mac

1. Big Data and Hadoop To study and learn about b...

Ant Design Blazor component library's routing reuse multi-tab function

Recently, there has been a growing demand for imp...

Nested display implementation of vue router-view

Table of contents 1. Routing Configuration 2. Vue...

How to use the Linux basename command

01. Command Overview basename - strip directories...

MySQL 5.7.18 installation tutorial under Windows

This article explains how to install MySQL from a...

Implementation of webpack-dev-server to build a local server

Table of contents Preface webpack-deb-server webp...

How to query the intersection of time periods in Mysql

Mysql query time period intersection Usage scenar...

MySQL permissions and database design case study

Permissions and database design User Management U...

How to switch directories efficiently in Linux

When it comes to switching directories under Linu...

Detailed explanation of the process of zabbix monitoring sqlserver

Let's take a look at zabbix monitoring sqlser...

Several ways to introduce pictures in react projects

The img tag introduces the image Because react ac...