nginx配置ssl验证

在上一篇博客中,我们只是通过nginx搭建了反向代理服务,由于需要在小程序中使用https服务,所以需要申请安全证书。

1.在所购买的域名商那申请免费的ssl证书,我买的是阿里的,所以直接在阿里上申请,并下载下来。具体申请方法可以参考:https://yq.aliyun.com/articles/637307,https://yq.aliyun.com/articles/641517?spm=a2c4e.11153940.blogcont637307.20.577e259d9An8Dk。

2.下载ssl的时候选择nginx,下载后解压会看到一个.key和.pem的文件,然后在服务器/etc/nginx/目录下,创建ssl文件夹,然后将这两个文件放入。

3.在nginx.conf中配置,一般默认会在/etc/nginx/nginx.conf,配置如下:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
 # Virtual Host Configs
        ##

        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
        server{
                #监听443端口
                listen 443;
                #对应的域名,把xxxxx.com改成你们自己的域名就可以了
                server_name xxxxx.com;
                ssl on;
                #从阿里云获取到的第一个文件的全路径
                ssl_certificate /etc/nginx/ssl/xxxxx.pem;
                #从阿里云获取到的第二个文件的全路径
                ssl_certificate_key /etc/nginx/ssl/xxxxx.key;
                ssl_session_timeout 5m;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
                ssl_prefer_server_ciphers on;
                location / {
                        # proxy_pass http://xxxxx;
                        # root /;
                        proxy_redirect off;
                        proxy_set_header  Host $host; #请求主机头字段,否则为服务器名称。
                        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for ;
                        proxy_set_header Accept-Encoding "";
                        proxy_set_header X-Real-IP $remote_addr;
                        add_header 'Access-Control-Allow-Origin' '*';
                        proxy_pass http://xxxxx.com:8000/;
                }
                location /wqgl/ {
                        proxy_redirect off;
                        proxy_pass http://xxxxx.com:3001/;
                }

        }
server{
                listen 80;
                server_name xxxxx.com;
                rewrite ^(.*)$ https://$host$1 permanent; # 把http的域名请求转成https
                #location / {
                        # proxy_pass http://xxxx;
                        # root /;
                        #proxy_redirect off;
                        #proxy_set_header  Host $host; #请求主机头字段,否则为服务器名称。
                        #proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for ;
                        #proxy_set_header Accept-Encoding "";
                        #proxy_set_header X-Real-IP $remote_addr;
                        #add_header 'Access-Control-Allow-Origin' '*';
                        #proxy_pass http://xxxxx.com:8000/;
                #}
                #location /wqgl/ {
                        #proxy_redirect off;
                        #proxy_pass http://xxxxx.com:3001/;
                #}
                # location / {
                       # try_files $uri $uri/ =404;
                # }


        }
}
#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

这样配置就成功,小程序就可以正常访问了。

posted @ 2019-03-18 19:26  纸鸢&红豆·绿豆  阅读(1379)  评论(0编辑  收藏  举报