1. 第一步:依赖环境安装
1 2 |
yum install pcre pcre-devel openssl openssl-devel zlib zlib-devel -y yum install gcc gcc-c++ make libtool -y |
2.第二步:下载nginx 版本
1 2 3 4 5 |
官网下载地址:http://nginx.org/download/ wget http://nginx.org/download/nginx-1.13.10.tar.gz wget https://mirrors.huaweicloud.com/nginx/nginx-1.22.1.tar.gz 解压下载nginx版本: tar -zxvf nginx-1.13.10.tar.gz |
3.创建www用户
1 2 |
groupadd www useradd -g www -s /sbin/nologin -M www |
3.第三步:编译安装
1 2 3 |
ngx_brotli #压缩模块不要的可以去掉 要的先下载到本地同级目录下 git clone https://github.com/google/ngx_brotli cd ngx_brotli && git submodule update --init |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#预编译 ./configure --prefix=/usr/local/nginx --user=www --group=www --prefix=/usr/local/nginx \ --with-cc-opt=-O2 \ --with-http_realip_module \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-stream \ --with-stream_ssl_module \ --add-module=./ngx_brotli ./configure --prefix=/usr/local/nginx --user=www --group=www --prefix=/usr/local/nginx \ --with-cc-opt=-O2 \ --with-http_realip_module \ --with-http_v2_module \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_gzip_static_module \ --with-stream \ --with-stream_ssl_module \ --with-http_sub_module \ --add-module=/root/ruanjian/nginx/nginx-1.13.10/substitutions4nginx-read-only 解: --prefix=/usr/local/nginx 是指定/usr/local/nginx 为安装目录 --with-http_ssl_module 是为了安装ssl模块(你也可以不装) |

1 |
make && make install |
测试是否安装成功
1 2 |
cd /usr/local/nginx ./sbin/nginx -t |

4.启动nginx
1 2 3 |
/usr/local/nginx/sbin/nginx 启动服务 停止服务: /usr/local/nginx/sbin/nginx -s stop curl localhost #访问默认nginx页面 |

5. 配置nginx开机自启动
1 2 3 4 5 6 7 |
vim /etc/rc.d/rc.local 增加一行 /usr/local/nginx/sbin/nginx 添加执行权限 chmod +x /etc/rc.d/rc.local 添加软连接:ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx 这样就不用每次都带路径了直接 nginx -s reload |
6. 配置 TLS 1.2 和 1.3
编辑 nginx.conf 文件,在 http 部分下添加以下内容:
1 |
ssl_protocols TLSv1.2 TLSv1.3; |
7. 配置限流:
使用 Nginx 的 limit_req_zone 指令来设置限制区域,并在需要进行限制的地方使用 limit_req 指令进行限制。例如,在nginx.conf 文件中添加以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
http { limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; server { ... location / { limit_req zone=one burst=5; ... } ... } } #以上配置将每秒最多允许一个请求,超过则返回 503 错误,同时允许瞬时突发 5 个请求。 |
8. 配置降级:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
使用 Nginx 的 proxy_next_upstream 指令来设置当后端服务器出现错误时是否继续尝试向下一个后端服务器转发请求。例如,在 nginx.conf 文件中添加以下内容: http { upstream backend { server backend1; server backend2; server backend3; } server { ... location / { proxy_pass http://backend; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; ... } ... } } #以上配置将在后端服务器出现错误时继续尝试向下一个后端服务器转发请求,直到所有后端服务器均出现错误或请求被成功处理。 |
9.配置grpc [nginx配置同时支持grpc(http2)方式传流以及http方式转发请求]
1 |
https://blog.csdn.net/xuxuxu1222/article/details/90175528 |
10.nginx 443端口找不到
1 2 |
nginx -s stop nginx |
- 本文固定链接: https://www.yoyoask.com/?p=521
- 转载请注明: shooter 于 SHOOTER 发表