nginx之https配置

  • 首先使用niginx -V 检测nginx是否安装了https模块。发现有ssl说明已经安装好了

  •  创建私钥证书。按照下面顺序执行这四条语句。
# 1、创建服务器私钥,命令会让你输入一个口令:
 openssl genrsa -des3 -out server.key 1024 
# 2、创建签名请求的证书(CSR):
 openssl req -new -key server.key -out server.csr 
# 3、在加载SSL支持的Nginx并使用上述私钥时除去必须的口令: 
openssl rsa -in server.key -out server_nopass.key 
# 4、最后标记证书使用上述私钥和CSR:
 openssl x509 -req -days 365 -in server.csr -signkey server_nopass.key -out server.crt
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# openssl genrsa -des3 -out server.key 1024 
Generating RSA private key, 1024 bit long modulus
.++++++
...............++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# openssl req -new -key server.key -out server.csr 
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^[[3~^[[3~^[[3~^[[3~^[[3~^[[3~^[[3~^[[3~^[[3~^C
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# openssl req -new -key server.key -out server.csr 
Enter pass phrase for server.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# 
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# 
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# 
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# 
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# openssl rsa -in server.key -out server_nopass.key 
Enter pass phrase for server.key:
writing RSA key
[root@iZ2ze4s0djlh8qcc8jvqiiZ ex]# openssl x509 -req -days 365 -in server.csr -signkey server_nopass.key -out server.crt
Signature ok
subject=/C=cn/L=Default City/O=Default Company Ltd
Getting Private key
  • 查看生成的文件

  • 再conf中配置https
server {
        listen       443 ssl;
        server_name  www.zxl.com;

        ssl_certificate    /usr/local/nginx/ex/server.crt; #证书地址
        ssl_certificate_key    /usr/local/nginx/ex/server_nopass.key; #秘钥地址

        location / {
           root html;
           index a.html;
        }

        location /ge {
           alias html;
           index a.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}
  • 访问https路径页面

  • 讲http访问方式重定向到https的方式

server {
        listen       80;
        server_name  www.zxl.com;

        rewrite ^(.*) https://$server_name$1 redirect;

        location / {
           root html;
           index a.html;
        }

        location /ge {
           alias html;
           index a.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}
  • 可以发现http://会跳到https

 

 

配置 Nginx 支持 HTTPS,您需要完成以下步骤: 1. 获取 SSL 证书:您可以从证书颁发机构(CA)购买 SSL 证书,或者使用免费的证书颁发机构(如 Let's Encrypt)生成证书。 2. 安装 SSL 证书:将 SSL 证书和私钥文件复制到服务器上的指定位置。通常,SSL 证书是以 `.crt` 或 `.pem` 格式存储,私钥文件是以 `.key` 格式存储。 3. 配置 Nginx:打开 Nginx配置文件(通常位于 `/etc/nginx/nginx.conf` 或 `/etc/nginx/conf.d/default.conf`),进行如下配置: ``` server { listen 443 ssl; server_name example.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/private_key.key; location / { # 其他配置项 } } ``` 替换 `example.com` 为您的域名,将 `/path/to/certificate.crt` 和 `/path/to/private_key.key` 替换为您实际的证书和私钥文件路径。 4. 配置 HTTPS 强制重定向(可选):如果您希望将所有 HTTP 请求自动重定向到 HTTPS,可以添加以下配置来实现: ``` server { listen 80; server_name example.com; return 301 https://$host$request_uri; } ``` 这将将所有 HTTP 请求重定向到 HTTPS。 5. 检查配置并重新加载 Nginx:运行 `nginx -t` 命令检查 Nginx 配置的语法是否正确。如果没有错误,运行 `nginx -s reload` 命令重新加载 Nginx 配置。 完成上述步骤后,您的 Nginx 将会配置为支持 HTTPS,您可以通过访问 `https://example.com` 来验证是否成功。请确保您已经打开服务器的防火墙并允许 HTTPS 流量通过。 请注意,这只是一个基本的 HTTPS 配置示例,具体配置可能因您的需求和环境而有所不同。建议阅读 Nginx 官方文档以获取更详细的配置说明。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值