nginx详解 http和https的配置

关于http rtmp 和hls 的配置发放请参看如下的链接我的另一篇文章:

https://blog.youkuaiyun.com/weixin_41970237/article/details/104523313
强烈推荐对如下文件进行保存, 一遍透彻的描述了nginx的结构。
https://www.cnblogs.com/dongye95/p/11096785.html

文章的开头说一下遇到的问题:**

1,关于https 配置部分的存放位置, 《http并行,还是http内部, 》《http service lision 80端口的后面 直接添加, 还是另外建立新的service部分》网络文章中各种描述用法都存在, 但是我是使用最后面,建立新的service部分测试成功的。
2,关于秘钥key 文件夹的命名, 和秘钥的存放位置,以下几种情况,

  1. /usr/share/nginx/html/conf 此目录下测试未成功。应该也能行
  2. /etc/nginx/pki/ 未进行测试。应该也可以
  3. /etc/nginx/cert. 秘钥保存在这里测试成功。

3,关于秘钥key文件的生成, nginx配置文件里面的ssl 指向的文件, 生成的文件当中选择没有密码文件成功

nginx 的结构思维导图

摘自于其他博客

nginx 配置结构图

摘自于文章开头博客
摘自文章开头博客

进入正题https 的搭建和配置,
首先 查看nginx配置

 nginx -V

configure arguments有 --with-http_ssl_module,表示安装了https模块。如果没有需要进行编译安装

`ssl https 秘钥安全文件的生成。此处注意文件的选用无密码的

cd /etc/nginx
mkdir cert
chmod -777 cert
openssl genrsa -des3 -out pt_ssl.key 2048 
openssl req -new -key pt_ssl.key -out pt_ssl.csr
openssl rsa -in pt_ssl.key -out pt_ssl_nopass.key
openssl x509 -req -days 365 -in pt_ssl.csr -signkey pt_ssl.key -out pt_ssl.crt
ls
pt_ssl.crt  pt_ssl.csr  pt_ssl.key  pt_ssl_nopass.key

nginx的配置文件

server {
	    listen 80 default_server;
	    listen 443 ssl;# 此处https 和 http 共存
	    server_name  localhost;
	    root         /usr/share/nginx/html;
	    ssl_certificate /etc/nginx/cert/pt_ssl.crt;
	    ssl_certificate_key /etc/nginx/cert/pt_ssl_nopass.key;
	    access_log logs/nginx.access.log main;#login路径不对nginx-t会报错
        charset utf-8; #避免中文乱码
        include /etc/nginx/default.d/*.conf;
        location / {
            root /; # 这里配置目录
            index  index.html index.htm;
            autoindex on;
            autoindex_exact_size on;
            autoindex_localtime on;
        }

nginx配置的检查(一定要善于运行这个检查工具很有帮助)

[root@localhost sbin]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost sbin]# nginx -s reload
nginx: [error] invalid PID number "" in "/run/nginx.pid"
[root@localhost sbin]# 
[root@localhost sbin]# nginx -s reload

重启nginx服务器

systemctl restart nginx

发现, http 和 https都可以使用了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值