nginx-1.21.0安装及SSL配置笔记记录
之前参照其他文章nginx配置https都好坑啊,,零零散散的总结一下,,也当作参考吧
nginx-1.21.0安装及SSL配置
1. 官网下载
(https://nginx.org/en/download.html)
2. 上传
上传到 /usr/local/ 下 ,(目录自定义)
或者直接:
wget https://nginx.org/download/nginx-1.20.1.tar.gz
3. 解压
tar -zxvf nginx-1.21.0.tar.gz
4. 进入nginx-1.21.0目录
cd /usr/local/nginx-1.21.0
5. 安装依赖
yum install -y gcc-c++ openssl openssl-devel pcre pcre-devel zlib zlib-devel
6. 使用nginx默认配置
./configure
7. 编译安装
make install
会在/usr/local/ 生成一个nginx目录。
8. 配置SSL
(也是在nginx1.21.0目录下执行)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module
make
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /usr/local/nginx/sbin/
8.1 修改配置文件
进入 编译生成 的nginx的conf配置文件 /usr/local/nginx/conf 下的 nginx.conf文件
cd /usr/local/nginx/conf
新建存放ssl证书目录
mkdir /usr/local/nginx/conf/ssl
vi nginx.conf
配置文件详情如下:
证书去各大云平台下载:例如腾讯云https://console.cloud.tencent.com/ssl/dsc/apply
可参考的配置
events {
worker_connections 1024; # 设置最大工作连接数
}
http {
include mime.types; # 包含 MIME 类型定义文件
default_type application/octet-stream; # 设置默认的 MIME 类型
sendfile on; # 启用高效的文件传输模式
keepalive_timeout 65; # 设置 keep-alive 超时时间
client_max_body_size 50m; # 设置客户端上传文件的最大尺寸
client_body_buffer_size 10m; # 设置客户端请求体的缓冲区大小
client_header_timeout 1m; # 设置客户端请求头的超时时间
client_body_timeout 1m; # 设置客户端请求体的超时时间
gzip on; # 启用 Gzip 压缩
gzip_min_length 1k; # 设置启用 Gzip 压缩的最小文件长度
gzip_buffers 4 16k; # 设置 Gzip 压缩的缓冲区大小
gzip_comp_level 4; # 设置 Gzip 压缩级别
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png; # 定义压缩的文件类型
gzip_vary on; # 启用响应头中的 Vary
ssl_session_timeout 5m; # 设置 SSL 会话超时时间
ssl_protocols TLSv1.2 TLSv1.3; # 定义支持的 SSL 协议版本
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!aNULL:!MD5:!ADH:!RC4; # 定义 SSL 加密套件
ssl_prefer_server_ciphers on; # 启用服务器优先选择加密套件
# 将所有 HTTP 请求重定向到 HTTPS
server {
listen 80; # 监听 80 端口(HTTP)
server_name coisini.cn www.coisini.cn api.coisini.cn admin.coisini.cn ws.coisini.cn upload.coisini.cn; # 定义要处理的域名
return 301 https://$host$request_uri; # 将请求永久重定向到 HTTPS
}
# 主网站(https://coisini.cn 和 https://www.coisini.cn)
server {
listen 443 ssl; # 监听 443 端口(HTTPS)
server_name coisini.cn www.coisini.cn; # 定义要处理的域名
ssl_certificate cert/coisini.cn_bundle.crt; # 指定 SSL 证书文件路径
ssl_certificate_key cert/coisini.cn.key; # 指定 SSL 证书私钥文件路径
location / {
# 替换所有 http://coisini.cn:9160 为 https://api.coisini.cn
sub_filter 'http://coisini.cn:9160' 'https://api.coisini.cn';
sub_filter_once off; # 允许多个子过滤器替换
root /usr/local/nginx/html/coisini-blog-view; # 设置网站根目录
index index.html index.htm; # 设置默认访问的文件
try_files $uri $uri/ /index.html; # 尝试按顺序访问文件
}
}
# API 服务器(https://api.coisini.cn)
server {
listen 443 ssl; # 监听 443 端口(HTTPS)
server_name api.coisini.cn; # 定义要处理的域名
ssl_certificate /usr/local/nginx/conf/cert/api.coisini.cn_bundle.crt; # 指定 SSL 证书文件路径
ssl_certificate_key /usr/local/nginx/conf/cert/api.coisini.cn.key; # 指定 SSL 证书私钥文件路径
location / {
# 替换所有 http://coisini.cn:9160 为 https://api.coisini.cn
sub_filter 'http://coisini.cn:9160' 'https://api.coisini.cn';
sub_filter_once off; # 允许多个子过滤器替换
proxy_pass http://coisini.cn:9160; # 反向代理到后端服务
proxy_set_header Host $host; # 设置代理请求头中的 Host
proxy_set_header X-Real-IP $remote_addr; # 设置代理请求头中的真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置代理请求头中的 X-Forwarded-For
proxy_set_header X-Forwarded-Proto $scheme; # 设置代理请求头中的协议(HTTP/HTTPS)
}
}
# 管理后台(https://admin.coisini.cn)
server {
listen 443 ssl; # 监听 443 端口(HTTPS)
server_name admin.coisini.cn; # 定义要处理的域名
ssl_certificate cert/admin.coisini.cn_bundle.crt; # 指定 SSL 证书文件路径
ssl_certificate_key cert/admin.coisini.cn.key; # 指定 SSL 证书私钥文件路径
location / {
root /usr/local/nginx/html/coisini-blog-admin; # 设置管理后台根目录
index index.html index.htm; # 设置默认访问的文件
try_files $uri $uri/ /index.html; # 尝试按顺序访问文件
}
}
# WebSocket 服务器(https://ws.coisini.cn)
server {
listen 443 ssl; # 监听 443 端口(HTTPS)
server_name ws.coisini.cn; # 定义要处理的域名
ssl_certificate cert/ws.coisini.cn_bundle.crt; # 指定 SSL 证书文件路径
ssl_certificate_key cert/ws.coisini.cn.key; # 指定 SSL 证书私钥文件路径
location / {
proxy_pass http://coisini.cn:9160/coisini/websocket; # 反向代理到后端 WebSocket 服务
proxy_http_version 1.1; # 使用 HTTP 1.1 协议
proxy_set_header Upgrade $http_upgrade; # 设置升级头
proxy_set_header Connection "Upgrade"; # 设置连接升级
proxy_set_header Host $host:$server_port; # 设置代理请求头中的 Host
proxy_set_header X-Real-IP $remote_addr; # 设置代理请求头中的真实 IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置代理请求头中的 X-Forwarded-For
proxy_set_header X-Forwarded-Proto $scheme; # 设置代理请求头中的协议(HTTP/HTTPS)
}
}
# 文件上传服务器(https://upload.coisini.cn)
server {
listen 443 ssl; # 监听 443 端口(HTTPS)
server_name upload.coisini.cn; # 定义要处理的域名
ssl_certificate cert/upload.coisini.cn_bundle.crt; # 指定 SSL 证书文件路径
ssl_certificate_key cert/upload.coisini.cn.key; # 指定 SSL 证书私钥文件路径
location / {
root /usr/local/coisini/blog/file/; # 设置文件上传的根目录
}
}
}
9.检测:
cd ..
./sbin/nginx -t
10. 重新加载配置文件:
/usr/local/nginx/sbin/nginx -s reload
11. 启动nginx
进入sbin路径
cd /usr/local/nginx/sbin
./nginx
12. 查看进程:
ps aux|grep nginx
然后https访问项目,没啥问题
13. 错误记录及解决:
问题: 证书路径错误,找不到
解决:去配置文件改一下证书路径就行,注意证书文件的存储位置
问题:
nginx: [error] invalid PID number “” in "/usr/local/nginx/logs/nginx.pid
解决:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
END