专业Nginx HTTPS配置指南 | 如何有效地设置您的Nginx

silverwq
2023-10-07 / 0 评论 / 160 阅读 / 正在检测是否收录...

下载 Nginx 服务证书

确定下文件有没有准备好,一份 SSL 证书、一份对应的私钥 KEY 文件(这里是用的 pem 与 key 文件, https://freessl.cn/ ,这个地方可以免费申请证书)上传到服务器上,编辑 conf 的时候注意你的这俩文件名字,跟跟服务器上的一致。

  1. ssl. pem;
  2. ssl. key; 这个有时候后缀是 ssl. pem,没什么关系

把这两个文件复制到对应目录里面。

使用acme自动部署:https://blog.freessl.cn/acme-quick-start/

先安装证书到本地

lv60mupq.png

然后安装到nginx

acme.sh --install-cert -d www.xiaoqiuyinboke.cn \
--key-file       /usr/local/nginx/conf/ssl/www_xiaoqiuyinboke_cn_key.pem  \
--fullchain-file /usr/local/nginx/conf/ssl/www_xiaoqiuyinboke_cn_cert.pem \
--reloadcmd     "/usr/local/nginx/sbin/nginx -s reload"

配置 nginx

server {
    # 监听的端口,nginx 1.15.0及以上版本,使用listen 443 ssl代替,1.15.0以下的使用listen 443
    listen       443 ssl;
    server_name  www.xiaoqiuyinboke.cn;
    #charset koi8-r;

    # ssl证书地址
    # 指定pem文件所在路径,如果写相对路径,必须把该文件和nginx.conf文件放到一个目录下。
    ssl_certificate    ssl/cert.pem;
    # 指定私钥文件key所在路径,如果写相对路径,必须把该文件和nginx.conf文件放到一个目录下。
    ssl_certificate_key  ssl/key.pem;
    # ssl验证相关配置
    # 缓存有效期
    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;

    #access_log  logs/host.access.log  main;
    root /usr/local/wqy/www/typecho/;

    location / {
    add_header Access-Control-Allow-Origin *;
   try_files $uri $uri/ /index.php?$query_string;
        index  index.html index.htm index.php;
        autoindex on;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ .+\.php($|/) {
   add_header Access-Control-Allow-Origin *;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_split_path_info  ^(.+\.php)(/.*)$;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        include        fastcgi.conf;
        #fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        #include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}

    # include ./conf/vhost/*.conf;
}
# 配置80端口重定向443端口
server {
    listen 80;
    server_name  www.xiaoqiuyinboke.cn;
    rewrite ^(.*)$ https://${server_name}$1 permanent;
}
0

评论 (0)

取消