zoukankan      html  css  js  c++  java
  • CentOS 7 配置nginx并默认强制使用https对http进行跳转

    1.安装nginx

    yum install nginx

    2.启动nginx服务

    service nginx start

    3.开启防火墙80端口,云服务器和本地虚拟服务器各有不同,不再赘述。

    4.访问你的域名,出现nginx页面,成功。

    关于https,首先需要下载ssl证书,本人使用的腾讯云服务器,域名也是腾讯上买的域名服务,所以直接在腾讯云申请了ssl dv证书。

    还有一种方式通过openssl自己生成ssl证书,个人没尝试过,网上教程颇多。

    5.ssl证书申请
    这里建议使用腾讯云申请免费ssl证书,一年免费,单域名模式下。当然如果有预算直接买泛域名的更好。

    注:可以申请多个单域名模式证书,比如,www.yourdomain.com,    blog.yourdomain.com,  这样就可以为二级域名设置https访问。

    大约半小时,腾讯就能审核通过。

    参照腾讯的说明验证通过后可以下载证书到本地,目录如下:

    E:DOOFEETECH00.公司IT运维www.yourdomain.COM
    │  www.yourdomain.com.csr
    │
    ├─Apache
    │      1_root_bundle.crt
    │      2_www.yourdomain.com.crt
    │      3_www.yourdomain.com.key
    │
    ├─IIS
    │      www.yourdomain.com.pfx
    │
    ├─Nginx
    │      1_www.yourdomain.com_bundle.crt
    │      2_www.yourdomain.com.key
    │
    └─Tomcat
            www.yourdomain.com.jks

    各种主流web服务器的都提供了,这里我们用nginx的。

    6.将ssl证书上传至服务器,个人单独建立了ssl文件目录。

    7.配置nginx.conf

    我忘了nginx默认的配置文件在哪个位置,使用如下命令
    nginx -t

    发现默认的nginx.conf 在/etc/nginx/nginx.con

    配置如下:

    首先修改对80端口的监听

    server {
            listen       80 default_server;
            listen       [::]:80 default_server;
            server_name  _;                           
            #经过试验发现,在server_name里面可以不指定域名,两种方式都OK
            #server_name www.yourdomain.com;          
            #rewrite     ^   https://$host$request_uri? permanent;
            rewrite ^(.*)$  https://$host$1 permanent; 
            root         /usr/share/nginx/html;
    
            # Load configuration files for the default server block.
            include /etc/nginx/default.d/*.conf;
    
            location / {
            }
    
            error_page 404 /404.html;
                location = /40x.html {
            }
    
            error_page 500 502 503 504 /50x.html;
                location = /50x.html {
            }
        }

    开启防火墙443端口,并启用对nginx中对443端口的监听

    server {
            listen       443 ssl http2 default_server;
            listen       [::]:443 ssl http2 default_server;
            #server_name  _;
            server_name  www.yourdomain.com;
            root         /usr/share/nginx/html;
    
            ssl_certificate "/home/ssl/keys/1_www.yourdomain.com_bundle.crt";
            ssl_certificate_key "/home/ssl/keys/2_www.yourdomain.com.key";
            ssl_session_cache shared:SSL:1m;
            ssl_session_timeout  10m;
            ssl_ciphers HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers on;
        
            
    
    #
    #        # Load configuration files for the default server block.
    #        include /etc/nginx/default.d/*.conf;
    #
            location / {
                 
                 tcp_nodelay on;
                 proxy_set_header Host $host;
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            }
    #
    #        error_page 404 /404.html;
    #            location = /40x.html {
    #        }
    #
    #        error_page 500 502 503 504 /50x.html;
    #            location = /50x.html {
    #        }
        }

    8.重启nginx或者重新加载配置

    重启 service nginx restart

    重载 nginx -s reload

    至此,使用http访问你的域名,会自动跳转到https。

    参考:

    https://www.jianshu.com/p/c0d2e5e77b0c

    https://www.jianshu.com/p/9523d888cf77

    https://www.jianshu.com/p/2a26539a9818

    https://blog.csdn.net/h330531987/article/details/81481426

    https://blog.csdn.net/zf5250/article/details/80429795

  • 相关阅读:
    SpringMVC拦截器的使用
    SQL必知必会 -------- 聚集函数、分组排序
    第9章 scrapy-redis分布式爬虫
    【转】eval()函数用法
    SQL必知必会 -------- 通配符、计算字段、函数
    第8章 scrapy进阶开发(2)
    SQL必知必会 -------- order by、where等
    SQL必知必会 -------- SELECT、注释
    第8章 scrapy进阶开发(1)
    office2016破解激活安装
  • 原文地址:https://www.cnblogs.com/huanghongbo/p/10112765.html
Copyright © 2011-2022 走看看