nginx
nginx(发音同engine x)是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like协议下发行。
nginx由俄罗斯的程序设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Rambler使用。
第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
nginx的特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
nginx的基本功能
静态资源的web服务器,能缓存打开的文件描述符
http、smtp、pop3协议的反向代理服务器
缓存加速、负载均衡
支持FastCGI(fpm,LNMP),uWSGI(Python)等
模块化(非DSO机制),过滤器zip、SSI及图像的大小调整
支持SSL
nginx的扩展功能
基于名称和IP的虚拟主机
支持keepalive
支持平滑升级
定制访问日志、支持使用日志缓冲区提高日志存储性能
支持URL重写
支持路径别名
支持基于IP及用户的访问控制
支持速率限制,支持并发数限制
nginx的应用类别
使用nginx结合FastCGI运行PHP、JSP、Perl等程序
使用nginx作反向代理、负载均衡、规则过滤
使用nginx运行静态HTML网页、图片
nginx与其他新技术的结合应用
nginx的模块与工作原理
nginx由内核和模块组成。其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
什么是URI和URL
URI:Uniform Resource Indentifier,统一资源标识符。用于定义全局范围内(包括但不仅限于互联网)去标记唯一的、定位一种资源访问路径的方式,或者命名方式,被称作统一资源标识符。这里的统一指的是路径格式上的统一。
URL:Uniform Resource Location,统一资源定位符,是URI的一个子集,用于描述在互联网上互联网资源的统一表示格式
nginx的模块分类
从结构上分为以下三种:
核心模块:HTTP模块、EVENT模块和MAIL模块等
基本模块:HTTP Access模块、HTTP FastCGI模块、HTTP Proxy模块和HTTP Rewrite模块
第三方模块:HTTP Upstream模块、Request Hash模块、Notice模块和HTTP Access Key模块
用户根据自己的需要开发的模块都属于第三方模块。正是有了如此多模块的支撑,nginx的功能才会如此强大
从功能上分为以下三种:
Handlers(处理器模块)。此类模块直接处理请求,并进行输出内容和修改headers信息等操作。handlers处理器模块一般只能有一个
Filters(过滤器模块)。此类模块主要对其他处理器模块输出的内容进行修改操作,最后由nginx输出
Proxies(代理器模块)。就是nginx的HTTP Upstream之类的模块,这些模块主要与后端一些服务比如fastcgi等操作交互,实现服务代理和负载均衡等功能
从基础模块来说包括以下三种:
核心模块:基本功能和指令,如进程管理和安全。常见的核心模块指令,大部分是放置在配置文件的顶部
事件模块:在Nginx内配置网络使用的能力。常见的events(事件)模块指令,大部分是放置在配置文件的顶部
配置模块:提供包含机制
注意:所谓基本模块,指的是nginx默认的功能模块,它们提供的指令,允许你使用定义nginx基本功能的变量,在编译时不能被禁用
总体来说,nginx模块分为:核心模块、事件模块、标准Http模块、可选Http模块、邮件模块、第三方模块和补丁等
nginx的工作原理
nginx的模块直接被编译进nginx,因此属于静态编译方式。
启动nginx后,nginx的模块被自动加载,与Apache不一样,首先将模块编译为一个so文件,然后在配置文件中指定是否进行加载。
在解析配置文件时,nginx的每个模块都有可能去处理某个请求,但是同一个处理请求只能由一个模块来完成。
nginx的进程架构:
启动nginx时,会启动一个Master进程,这个进程不处理任何客户端的请求,主要用来产生worker线程,一个worker线程用来处理n个request。
下图展示了nginx模块一次常规的HTTP请求和响应的过程
Web服务器请求的步骤
(1)建立连接 — 接受一个客户端连接,或者如果不希望与这个客户端建立连接,就将其关闭。
(2)接收请求 — 从网络中读取一条 HTTP 请求报文。
(3)处理请求 — 对请求报文进行解释,并采取行动。
(4)访问资源 — 访问报文中指定的资源。
(5)构建响应 — 创建带有正确首部的 HTTP 响应报文。
(6)发送响应 — 将响应回送给客户端。
(7)记录事务处理过程 — 将与已完成事务有关的内容记录在一个日志文件中。
网站放置位置
apche使用yum安装网站位置放置在/var/www/html
源码安装放置在安装位置下的html(/usr/local/apache/htdocs)
nginx使用yum安装网站位置放置在/usr/share/nginx/html
使用源码放在安装位置下的/usr/local/nginx
nginx的安装与配置
安装nginx
#创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
1
#安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@localhost ~]# yum -y groups mark install 'Development Tools'
上次元数据过期检查:2:59:11 前,执行于 2021年10月25日 星期一 01时04分32秒。
依赖关系解决。
=============================================================
软件包 架构 版本 仓库 大小
=============================================================
安装组:
Development Tools
事务概要
=============================================================
完毕!
#创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
--2021-10-25 04:06:16-- http://nginx.org/download/nginx-1.12.0.tar.gz
正在解析主机 nginx.org (nginx.org)...
#编译安装
[root@localhost src]# ls
debug kernels nginx-1.12.0.tar.gz
[root@localhost src]# tar xf nginx-1.12.0.tar.gz
[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@localhost nginx-1.12.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#nginx配置,服务控制方式,使用nginx命令
#配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# . /etc/profile.d/nginx.sh
#服务控制方式,使用nginx命令
#使用 nginx -t 检查配置文件语法
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#使用nginx -v 输出nginx的版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.20.1
#使用nginx -c 指定配置文件的路径
[root@localhost ~]# nginx -s stop ; nginx -c /opt/nginx.conf ##直接停掉然后启动
[root@localhost ~]# ps -ef|grep nginx
root 99290 1 0 03:32 ? 00:00:00 nginx: master process nginx -c /opt/nginx.conf
nginx 99291 99290 0 03:32 ? 00:00:00 nginx: worker process
nginx 99292 99290 0 03:32 ? 00:00:00 nginx: worker process
root 101138 1653 0 03:33 pts/0 00:00:00 grep --color=auto nginx
#使用nginx -s 发送服务控制信号,可选值有{stop|quit|reopen|reload}
[root@localhost ~]# nginx -s quit
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
nginx的配置文件详解
主配置文件:/usr/local/nginx/conf
- 默认启动nginx时,使用的配置文件是:安装路径/conf/nginx.conf文件
- 可以在启动nginx时通过-c选项来指定要读取的配置文件
Nginx配置文件示例
[root@localhost ~]# vim /usr/local/nginx/conf
# 全局块
user www-data; ##用户
worker_processes 2; ## 默认1,一般建议设成CPU核数1-2倍
error_log logs/error.log; ## 错误日志路径
pid logs/nginx.pid; ## 进程id
# Events块
events {
# 使用epoll的I/O 模型处理轮询事件。
# 可以不设置,nginx会根据操作系统选择合适的模型
use epoll;
# 工作进程的最大连接数量, 默认1024个
worker_connections 2048;
# http层面的keep-alive超时时间
keepalive_timeout 60;
# 客户端请求头部的缓冲区大小
client_header_buffer_size 2k;
}
# http块
http {
include mime.types; # 导入文件扩展名与文件类型映射表
default_type application/octet-stream; # 默认文件类型
# 日志格式及access日志路径
log_format main '$remote_addr - $remote_user [$time_local] $status '
'"$request" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
# 允许sendfile方式传输文件,默认为off。
sendfile on;
tcp_nopush on; # sendfile开启时才开启。
# http server块
# 简单反向代理
server {
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
# 转发动态请求到web应用服务器
location / {
proxy_pass http://127.0.0.1:8000;
deny 192.24.40.8; # 拒绝的ip
allow 192.24.40.6; # 允许的ip
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# 负载均衡
upstream backend_server {
server 192.168.0.1:8000 weight=5; # weight越高,权重越大
server 192.168.0.2:8000 weight=1;
server 192.168.0.3:8000;
server 192.168.0.4:8001 backup; # 热备
}
server {
listen 80;
server_name big.server.com;
access_log logs/big.server.access.log main;
charset utf-8;
client_max_body_size 10M; # 限制用户上传文件大小,默认1M
location / {
# 使用proxy_pass转发请求到通过upstream定义的一组应用服务器
proxy_pass http://backend_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
error_log里的位置和级别能有以下可选项:
位置 | 级别 |
---|---|
file stderr syslog:server=address[,parameter=value] memory:size | debug:若要使用debug级别,需要在编译nginx时使用–with-debug选项 info notice warn error crit alert emerg |
正常运行必备的配置参数
user USERNAME [GROUPNAME]; //指定运行worker进程的用户和组
pid /path/to/pid_file; //指定nginx守护进程的pid文件
worker_rlimit_nofile number; //设置所有worker进程最大可以打开的文件数,默认为1024
worker_rlimit_core size; //指明所有worker进程所能够使用的总体的最大核心文件大小,保持默认即可
优化性能的配置参数
worker_processes n; //启动n个worker进程,这里的n为了避免上下文切换,通常设置为cpu总核心数-1或等于总核心数
worker_cpu_affinity cpumask ...; //将进程绑定到某cpu中,避免频繁刷新缓存
//cpumask:使用8位二进制表示cpu核心,如:
0000 0001 //第一颗cpu核心
0000 0010 //第二颗cpu核心
0000 0100 //第三颗cpu核心
0000 1000 //第四颗cpu核心
0001 0000 //第五颗cpu核心
0010 0000 //第六颗cpu核心
0100 0000 //第七颗cpu核心
1000 0000 //第八颗cpu核心
timer_resolution interval; //计时器解析度。降低此值,可减少gettimeofday()系统调用的次数
worker_priority number; //指明worker进程的nice值
nginx作为web服务器时使用的配置:http{}段的配置参数
http{…}:配置http相关,由ngx_http_core_module模块引入。nginx的HTTP配置主要包括四个区块,结构如下:
http {//协议级别
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
upstream {//负载均衡配置
...
}
server {//服务器级别,每个server类似于httpd中的一个<VirtualHost>
listen 80;
server_name localhost;
location / {//请求级别,类似于httpd中的<Location>,用于定义URL与本地文件系统的映射关系
root html;
index index.html index.htm;
}
}
}
http{}段配置指令:
server {}:定义一个虚拟主机,示例如下:
server {
listen 80;
server_name www.idfsoft.com;
root "/vhosts/web";
}
listen:指定监听的地址和端口
listen address[:port];
listen port;
server_name NAME [...]; 后面可跟多个主机,名称可使用正则表达式或通配符
当有多个server时,匹配顺序如下:
先做精确匹配检查
左侧通配符匹配检查,如*.idfsoft.com
右侧通配符匹配检查,如mail.*
正则表达式匹配检查,如~ ^.*.idfsoft.com$
default_server
root path; 设置资源路径映射,用于指明请求的URL所对应的资源所在的文件系统上的起始路径
alias path; 用于location配置段,定义路径别名
index file; 默认主页面
index index.php index.html;//谁优先谁在前面
error_page code […] [=code] URI | @name
根据http响应状态码来指明特用的错误页面,例如
error_page 404 /404_customed.html
[=code]:以指定的响应码进行响应,而不是默认的原来的响应,默认表示以新资源的响应码为其响应码,例如
error_page 404 =200 /404_customed.html
log_format 定义日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
//注意:此处可用变量为nginx各模块内建变量
错误页面配置
开启nginx,在页面中输入个不存在的文件会出现404的提醒页面
实例:
#取消注释404
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
error_page 404 /404.html; #取消注释
# redirect server error pages to the static page /50x.html
#现在网页上下载一个公益网站传到html里
[root@localhost conf]# cd /usr/local/nginx/html/
[root@localhost html]# ls
50x.html index.html index.php
[root@localhost html]# ls
50x.html 中国公益网_files 中国公益网.html index.html index.php
#修改页面名称
[root@localhost html]# mv 中国公益网.html 404.html
[root@localhost html]# ls
404.html 50x.html 中国公益网_files index.html index.php
#缓存下再进行访问
[root@localhost html]# nginx -s reload
刷新后页面:
虽然图是这样的,但是访问网站还是网络还是显示的是404,以下操作就是把网络修改为200
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# vim nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
error_page 404 =200 /404.html; #直接标明404=200
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
#缓存下再进行访问
[root@localhost html]# nginx -s reload
平滑升级
平滑升级:不能执行安装操作
1.获取老版本的编译参数 -V
2.获取新版本或新功能的软件包
3.对新功能或新版本的软件包进行编译
4.备份老版本
5.停掉老程序并用新程序使用老程序的配置文件进行启动
//获取新版本的软件包或功能包,下载地址 github.com
[root@localhost conf]# yum -y install unzip
[root@localhost src]# ls
debug mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
echo-nginx-module-master.zip nginx-1.20.1.tar.gz
kernels php-8.0.10.tar.xz
[root@localhost src]# tar xf nginx-1.20.1.tar.gz
[root@localhost src]# unzip echo-nginx-module-master.zip
//获取现有的程序编译的参数
[root@localhost local]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-3) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log
//将新功能或新版本进行编译
[root@localhost src]# cd nginx-1.20.1
[root@localhost nginx-1.20.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-debug --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --add-module=../echo-nginx-module-master
[root@localhost nginx-1.20.1]# make
//备份原程序
[root@localhost nginx-1.20.1]# ll objs/nginx /usr/local/nginx/sbin/nginx
-rwxr-xr-x. 1 root root 6831056 10月 27 18:46 objs/nginx
-rwxr-xr-x. 1 root root 6308640 10月 26 18:39 /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.20.1]# cp /usr/local/nginx/sbin/nginx /opt/
[root@localhost nginx-1.20.1]# ls /opt/
data nginx
//停掉旧程序使用新程序,并测试
[root@localhost nginx-1.20.1]# nginx -s stop;objs/nginx -c /usr/local/nginx/conf/nginx.conf
[root@localhost conf]# vi nginx.conf
location /awm {
echo 'my name is awm'; #添加
}
#error_page 404 /404.html;
[root@localhost nginx-1.20.1]# objs/nginx -s reload
//测试成功,覆盖旧程序
[root@localhost nginx-1.20.1]# \cp objs/nginx /usr/local/nginx/sbin/
//停掉新程序,去使用覆盖的程序
[root@localhost nginx-1.20.1]# objs/nginx -s stop;nginx
location区段
location区段,通过指定模式来与客户端请求的URI相匹配
//功能:允许根据用户请求的URI来匹配定义的各location,匹配到时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
//语法:location [ 修饰符 ] pattern {......}
常用修饰符说明:
修饰符 | 功能 |
---|---|
= | 精确匹配 |
~ | 正则表达式模式匹配,区分大小写 |
~* | 正则表达式模式匹配,不区分大小写 |
^~ | 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式 |
@ | 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或error_page等 |
没有修饰符表示必须以指定模式开始
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
location /test { #匹配/test下的所有
echo "test";
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload
[root@nginx html]# curl http://192.168.230.132
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
//那么如下内容就可正确匹配:
[root@localhost conf]# curl http://192.168.230.132/test
test
[root@localhost conf]# curl http://192.168.230.132/test/
test
精确查询
[root@localhost conf]# vim nginx.conf
location /test {
echo "test";
}
location = /test {
echo "精确匹配"; #添加
}
[root@localhost conf]# nginx -s reload
//可以匹配到
[root@localhost conf]# curl http://192.168.230.132/test
精确匹配
//一下内容匹配不到
[root@localhost conf]# curl http://192.168.230.132/test/
test
[root@localhost conf]# curl http://192.168.230.132/test/mmhh
test
以下是重点:
查找顺序和优先级:由高到底依次为
带有=的精确匹配优先
正则表达式按照他们在配置文件中定义的顺序
带有^~修饰符的,开头匹配
带有~或 ~*修饰符的,如果正则表达式与URI匹配
没有修饰符的精确匹配
优先级的顺序:
( location = 路径 ) --> ( location ^~ 路径 ) --> ( location ~ 正则 ) --> ( location ~* 正则 ) --> ( location 路径 )
访问控制
[root@localhost conf]# vim nginx.conf
location /test {
root html;
index index.html; #添加
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
[root@localhost tset]# pwd
/usr/local/nginx/html/tset
[root@localhost tset]# vim index.html
[root@localhost tset]# cat index.html
naicha
[root@localhost conf]# nginx -s reload
[root@localhost conf]# vim nginx.conf
location /tset {
#allow 192.168.230.1 #相当于白名单,设置白名单一般后面还会跟 deny all;才有意义
deny 192.168.230.1; #相当于黑名单
root html;
index index.html;
}
#error_page 404 /404.html;
[root@localhost conf]# nginx -s reload
再次访问后
用户认证
[root@localhost conf]# vim nginx.conf
location /test {
auth_basic "我想喝奶茶";
auth_basic_user_file ".pass";
root html;
index index.html;
}
#error_page 404 /404.html;
[root@localhost conf]# yum -y install httpd-tools
//不是系统用户,是用来访问登录的用户
[root@localhost conf]# htpasswd -c -m .pass admin
New password:
Re-type new password:
Adding password for user admin
[root@localhost conf]# cat .pass
admin:$apr1$wPAs4hYY$tSD6HTkLdvaLFxbzdTe.r1
https
[root@localhost conf]# mkdir ssl
[root@localhost conf]# cd ssl
[root@localhost conf]# mkdir -p /etc/pki/CA
[root@localhost ssl]# cd /etc/pki/CA
//生成密钥
[root@localhost CA]# mkdir private && (umask 077;openssl genrsa -out private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
................................+++++
.................................................................................................+++++
e is 65537 (0x010001)
[root@localhost CA]# ls
private
//生成自签证书
[root@localhost CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
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) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:rumtime
Organizational Unit Name (eg, section) []:rumtime
Common Name (eg, your name or your server's hostname) []:tset.runtime.com
Email Address []:1@2.com
[root@localhost CA]# mkdir certs newcerts crl
[root@localhost CA]# ls
cacert.pem certs crl newcerts private
[root@localhost CA]# touch index.txt && echo 01 > serial
[root@localhost CA]# ls
cacert.pem certs crl index.txt newcerts private serial
//生成密钥
[root@localhost ssl]# (umask 077;openssl genrsa -out nginx.key 2048)
Generating RSA private key, 2048 bit long modulus (2 primes)
......+++++
....................................................+++++
e is 65537 (0x010001)
[root@localhost ssl]# ls
nginx.key
[root@localhost ssl]# openssl req -new -key nginx.key -days 365 -out nginx.csr
Ignoring -days; not generating a certificate
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) []:HB
Locality Name (eg, city) [Default City]:WH
Organization Name (eg, company) [Default Company Ltd]:rumtime
Organizational Unit Name (eg, section) []:rumtime
Common Name (eg, your name or your server's hostname) []:tset.runtime.com
Email Address []:1@2.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@localhost ssl]# ll
总用量 8
-rw-r--r--. 1 root root 1033 10月 27 22:23 nginx.csr
-rw-------. 1 root root 1679 10月 27 22:20 nginx.key
[root@localhost ssl]# openssl ca -in nginx.csr -out nginx.crt -days 365
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
Serial Number: 1 (0x1)
Validity
Not Before: Oct 27 14:26:54 2021 GMT
Not After : Oct 27 14:26:54 2022 GMT
Subject:
countryName = CN
stateOrProvinceName = HB
organizationName = rumtime
organizationalUnitName = rumtime
commonName = tset.runtime.com
emailAddress = 1@2.com
X509v3 extensions:
X509v3 Basic Constraints:
CA:FALSE
Netscape Comment:
OpenSSL Generated Certificate
X509v3 Subject Key Identifier:
E4:31:1D:62:46:1B:33:0E:49:27:D8:2E:AC:93:7D:CF:4E:1C:53:D8
X509v3 Authority Key Identifier:
keyid:3A:BE:A2:4E:93:D1:EA:11:7C:4B:F6:68:03:F8:67:33:D4:71:BB:44
Certificate is to be certified until Oct 27 14:26:54 2022 GMT (365 days)
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit? [y/n]y
Write out database with 1 new entries
Data Base Updated
[root@localhost ssl]# rm -rf nginx.csr
[root@localhost ssl]# ls
nginx.crt nginx.key
[root@localhost conf]# vim nginx.conf
server { #这些都取消注释
listen 443 ssl;
server_name test.runtime.com;
ssl_certificate ssl/nginx.crt; #修改证书的位置
ssl_certificate_key ssl/nginx.key; #修改
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
}
[root@localhost conf]# nginx -s reload
[root@localhost conf]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:443 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*