【备忘】Nginx配置https

前言: 

一般情况下网站使用 http协议就可以了,方便快捷,但有些场景是必须要用https协议的,例如支付场景,像微信支付回调就要求必须使用https的域名地址。

截图证明:

 所以说本文就把自己的操作思路写出来做一个备忘,加深记忆,如果帮助了其它人,那也更好了。

操作步骤:

  1. 先购买https证书,各大厂商都有卖,例如阿里云、腾讯云、华为云。
  2. 把证书解压出来,找到这两个文件
  3. 把证书都上传到Linux服务器的目录中,如:/etc/nginx/ssl/,上传就行不需要做什么操作。
  4. 配置nginx,找到你的网站配置文件进行编辑,增加以下代码
            #监听https默认端口,不是80哦!
            listen       443 ssl;
    
            #兼容http访问,方便做非https跳转https功能
            listen       80;
            #后面是你的证书名称
            ssl_certificate /etc/nginx/ssl/test.hnlxwh.cn.crt;
            ssl_certificate_key /etc/nginx/ssl/test.hnlxwh.cn.key;

     

  5. 配置http自动跳转https,非必须。

      if ($scheme = http) {
           rewrite ^(.*)$  https://$host$1 permanent;
      }

参考:

#start
server {
        listen       443 ssl;

        listen       80;

        ssl_certificate /etc/nginx/ssl/test.hnlxwh.cn.crt;
        ssl_certificate_key /etc/nginx/ssl/test.hnlxwh.cn.key;

        
        server_name  zdht.hnlxwh.cn;
        root  /home/html/zc_game_admin/public;

        index index.php index.html;

        client_max_body_size 10M;

      
        if ($scheme = http) {
            rewrite ^(.*)$  https://$host$1 permanent;
        }
        #autoindex  off;

       


location / {

        if (!-e $request_filename) {
       
            rewrite  ^(.*)$  /index.php?s=/$1  last;
            break;

        }
}

location ~ \.php(.*)$ { # 正则匹配.php后的pathinfo部分
    root /home/html/zc_game_admin/public;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
    fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
    include        fastcgi_params;
}
# 配置资源文件后缀
 location  ~ .*\.(jpg|jpeg|gif|png|ico|css|js|txt|ttf|woff|woff2)$
  {
      root /home/html/zc_game_admin/public/;
      proxy_temp_path /home/html/zc_game_admin/public/;
  }


}




# end

Guess you like

Origin blog.csdn.net/qq_15941409/article/details/119028395
Recommended