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

  • 2021-08-28 21:31:35
  • OfStack

Nginx configuration can be accessed in both http and https with the same domain name, and 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; // Ali after downloading the application ssh Offered pem
ssl_certificate_key /usr/local/nginx/cert/21402058063066221.key;// Ali after downloading the application ssh Offered 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/ Website Directory ;

include laravel.conf; // Okay, here's the laravel Configuration, no 1 It will suit you, please or omit 
#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 above listen 80;;

listen 443 ssl; Open Port 80

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

Redirecting http to https uses the redirect command of nginx. So how should I write redirection? Previous versions of nginx may have used the following similar format.
That is to say, add another virtual machine server and one port 80


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

Rewriting still different versions may be as follows


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

Or


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

Now the new version of nginx has been written in a different way, and these are no longer recommended. At present, there may be many articles written on the Internet about the first kind.

The following is the latest support for redirecting 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 run, so maybe it doesn't support this writing...

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


server
{
#listen 80;
listen 443;
ssl on;
server_name domain.com; // Your domain name 
index 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;
}


Related articles: