Centos 7.5 下安装 Nginx 1.20.1

2021-07-03  本文已影响0人  轻飘飘D

1.安装补充工作

yum -y install epel-release
yum -y install net-tools wget nscd lsof

2.DNS缓存

# 编辑/ etc/ resolv. conf 配置 DNS 服务器, 
# 打开 NSCD 服务, 缓存 DNS, 提高 域名 解析 响应 速度。
[root@xag132 src]# cat /etc/resolv.conf
# Generated by NetworkManager
nameserver 8.8.8.8
nameserver 8.8.4.4

#启动 NSCD 服务
[root@xag132 src]# systemctl start nscd.service
[root@xag132 src]# systemctl enable nscd.service

  1. 下载稳定版(版本号中间20为偶数表示稳定版)
[root@xag132 src]# 
wget http://nginx.org/download/nginx-1.20.1.tar.gz

#解压安装包
[root@xag132 src]# tar zxmf nginx-1.20.1.tar.gz
[root@xag132 src]# cd nginx-1.20.1
[root@xag132 nginx-1.20.1]# ./configure --help

  --help                             print this message
  --prefix=PATH                      set installation prefix
 。。。
  --with-debug                       enable debug logging

4.安装编译工具依赖库

yum -y install gcc pcre-devel zlib-devel openssl-devel libxml2-devel
yum -y install libxslt-devel gd-devel GeoIP-devel jemalloc-devel
yum -y install libatomic_ops-devel perl-devel perl-ExtUtils-Embed

[root@xag132 nginx-1.20.1]# groupadd nginx
[root@xag132 nginx-1.20.1]# useradd nginx -g nginx -s /sbin/nologin -M

5.编译安装

[root@xag132 nginx-1.20.1]#
./configure --prefix=/usr/local/nginx \
 --user=nginx \
 --group=nginx \
 --with-threads \
 --with-file-aio \
 --with-http_ssl_module \
 --with-http_v2_module \
 --with-http_realip_module \
 --with-http_addition_module \
 --with-http_xslt_module=dynamic \
 --with-http_image_filter_module=dynamic \
 --with-http_geoip_module=dynamic \
 --with-http_sub_module \
 --with-http_dav_module \
 --with-http_flv_module \
 --with-http_mp4_module \
 --with-http_gunzip_module \
 --with-http_gzip_static_module \
 --with-http_auth_request_module \
 --with-http_random_index_module \
 --with-http_secure_link_module \
 --with-http_degradation_module \
 --with-http_slice_module \
 --with-http_stub_status_module \
 --with-stream=dynamic \
 --with-stream_ssl_module \
 --with-stream_realip_module \
 --with-stream_geoip_module=dynamic \
 --with-stream_ssl_preread_module \
 --with-compat \
 --with-pcre-jit 

[root@xag132 nginx-1.20.1]#
make && make install

6.环境 配置

cat >/etc/profile  << EOF
PATH=$PATH:/usr/local/nginx/sbin
EOF

source /etc/profile

#检查
[root@xag132 nginx-1.20.1]# 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@xag132 nginx-1.20.1]# nginx -v
nginx version: nginx/1.20.1

7.解决中文注解乱码问题

[root@xag132 run]# locale -a |grep "zh_CN"
zh_CN
zh_CN.gb18030
zh_CN.gb2312
zh_CN.gbk
zh_CN.utf8

[root@xag132 run]# vim /etc/locale.conf
[root@xag132 run]# cat /etc/locale.conf
#LANG="en_US.UTF-8"
LANG="zh_CN"
[root@xag132 run]# source  /etc/locale.conf

[解决CentOS7 VIM显示中文乱码的问题]
使用vim的时候有中文乱码,问题出在vim上,对于CentOS应该修改/etc/vimrc文件,在该文件头上添加下面四行代码
----------------------------------------------------
set fileencodings=utf-8,gb2312,gbk,gb18030  
set termencoding=utf-8  
set fileformats=unix  
set encoding=prc 

8.注册系统服务

[root@xag132 run]# cat /usr/lib/systemd/system/nginx.service
[Unit]                          # 记录service文件的通用信息
Description= The Nginx HTTP and reverse proxy server    # Nginx 服务描述信息
After=network.target remote-fs.target nss-lookup.target # Nginx 服务启动依赖,在指定服务之后启动

[Service]                       # 记录service文件的service 信息 
Type=forking                        # 标准UNIX Daemon 使用的启动方式
PIDFile=/usr/local/nginx/logs/nginx.pid                     # Nginx服务的 pid 文件位置
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid         # Nginx服务启动前删除旧的 pid 文件
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q      # Nginx服务启动前执行配置文件 检测
ExecStart=/usr/local/nginx/sbin/nginx -g "pid /usr/local/nginx/logs/nginx.pid;"         # 启动Nginx 服务
ExecReload=/usr/local/nginx/sbin/nginx -t -q                # Nginx 服务重启前执行配置文件 检测
ExecReload=/usr/local/nginx/sbin/nginx -s reload -g "pid /usr/local/nginx/logs/nginx.pid;"  # 重启 Nginx 服务
ExecStop=/bin/kill -s HUP $ MAINPID                     # 关闭 Nginx 服务
KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true

[Install]                               # 记录 service 文件的安装信息
WantedBy=multi-user.target                      # 多用户环境下启用

[root@xag132 run]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description= The Nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service] 
Type=forking
PDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -q
ExecStart=/usr/local/nginx/sbin/nginx -g "pid /usr/local/nginx/logs/nginx.pid;"
ExecReload=/usr/local/nginx/sbin/nginx -t -q
ExecReload=/usr/local/nginx/sbin/nginx -s reload -g "pid /usr/local/nginx/logs/nginx.pid;"
ExecStop=/bin/kill -s HUP $ MAINPID
KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl enable nginx  # 将 Nginx 服务 注册 为 系统 启动 后 自动 启动 
systemctl start nginx   # 启动 Nginx 服务 命令 
systemctl reload nginx  # reload Nginx 服务 命令 
systemctl stop nginx    # stop Nginx 服务 命令 
systemctl status nginx  # 查看 Nginx 服务 运行 状态 命令

9.1配置 [root@xag132 conf]# cat nginx.conf

daemon on;               #以守护进程的方式运行 Nginx 
#pid logs/nginx.pid;     #主进程 ID 记录 在 logs/nginx.pid中(和 8.注册系统服务中重复) 
user nginx nginx;        #工作进程 运行用户为 nginx

#Nginx 根据 CPU 的 内核 数 生成 等 数量 的 工作 进程。
worker_processes auto;
#工作进程 CPU 绑定指令
worker_cpu_affinity auto;
# 错误 日志 输出 级别 为 warn
error_log logs/nginx_error.log warn; 
#默认情况下包括一个32个线程的线程池,长度为65536的请求队列
thread_pool default threads=32 max_queue=65536;
#aio threads=default;
#启用 pcre_ jit 技术
pcre_jit on; 

# 定时器 周期 为 100 毫秒
timer_resolution 100ms;
#工作 进程 系统 优先级 为- 5
worker_priority -5;
#所有 工作 进程 的 最大 连接 数 是 65535
worker_rlimit_nofile 65535;
#工作 进程 关闭 等待 时间 是 10 秒
worker_shutdown_timeout 10s;
# 互斥 锁 文件 的 位置 是 logs/ nginx.lock
lock_file logs/nginx.lock;
# 工作 进程 工作 目录 是 logs
##working_directory logs
# 调试 点 模式 为stop 
debug_points stop;  
# 崩溃 文件 大小 为 800MB
worker_rlimit_core 800m; 

events 
{
#每个 工作 进程 的 最大 连接 数 是 65535
worker_connections 65535; 
#指定 事件 模型 为 epoll
use epoll; 
#启用 互斥 锁 模式 的 进程 调度
accept_mutex on; 
#互斥 锁 模式 下 进程 等待 时间 为 300 毫秒
accept_mutex_delay 300ms; 
#启用 支持 多 连接
multi_accept on;  
#完成 异步 操作 最大数 为 128 
worker_aio_requests 128;
#调试 指定 连接 的 IP 地址 和 端口 是 192. 168. 0. 0/ 24
#debug_connection 192.168.0.0/24;  
}

http 
{
 include proxy.conf; 
 include vhosts/*.conf;
}

9.2 [root@xag132 conf]# cat proxy.conf (反向代理配置)

include       mime.types;
default_type  application/octet-stream;

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;
charset utf-8;

sendfile       on;
tcp_nopush     on;
tcp_nodelay    on;
keepalive_timeout  65;
  
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
     
client_header_timeout 600s;
client_body_timeout 600s;
    
client_max_body_size 100m;           
client_body_buffer_size 256k;         
    
gzip  on;
gzip_min_length  1k;
gzip_buffers     4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types       text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on;

proxy_buffering on; 
#启用 响应 数据 缓冲区 

proxy_buffers 8 8k; 
#设置每个 HTTP 请求 读取 上游 服务器 响应 数据 缓冲区 的 大小 为 64KB 

proxy_buffer_size 8k; 
#设置每个 HTTP 请求 读取 响应 数据 第一 部分 缓冲区 的 大小 为 8KB

proxy_busy_buffers_size 16k; 
#接收 上游 服务器 返回 响应 数据 时, 同时 用于 向 客户 端 发送 响应 的 缓 #冲 区 的 大小 为 16KB 

proxy_limit_rate 0; 
#不限 制 每个 HTTP 请求 每秒 读取 上游 服务器 响应 数据 的 流量 

proxy_request_buffering on; 
#启用 客户 端 HTTP 请求 读取 缓冲区 功能 

proxy_http_version 1.1; 
#使用 HTTP 1. 1 版本 协议 与 上游 服务器 建立 通信 

proxy_connect_timeout 5s; 
#设置与 上游 服务器 建立 连接 的 超时 时间 为 5s 

proxy_intercept_errors on; 
#拦截 上游 服务器 中 响应 码 大于 300 的 响应 处理 

proxy_read_timeout 60s; 
#从 上游 服务器 获取 响应 数据 的 间隔 超时 时间 为 60s

proxy_send_timeout 60s; 
#向上游 服务器 发送 请求 的 间隔 超时 时间 为 60s 

#设置发送 给 上游 服务器 的 头 属性 字段 Host 为 客户 端 请求 头头 字段 Host 的 值 
proxy_set_header Host $host:$server_port; 

#设置发送 给 上游 服务器 的 头 属性 字段 Referer 为 客户 端 请求 头头 字段 的 值 Host 
proxy_set_header Referer $http_referer; 

#设置发送 给 上游 服务器 的 头 属性 字段 Cookie 为 客户 端 请求 头头 字段 的 值Host 
proxy_set_header Cookie $http_cookie; 

#设置发送 给 上游 服务器 的 头 属性 字段 X- Real-IP 为 客户 端 的 IP 
proxy_set_header X-Real-IP $remote_addr; 

#设置发送 给 上游 服务器 的 头 属性 字段 X-Forwarded-For 为 客户 端 请求 头 的 X- Forwarded- For 的 
#值, 如果 没有 该 字段, 则 等于 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

#设置发送 给 上游 服务器 的 头 属性 字段 X-Forwarded-Proto 为 请求 协议 的 值 
proxy_set_header X-Forwarded-Proto $scheme;

9.3 cat 81.conf
访问132机器的81端口,反向代理到129机器的81端口

server 
{
    listen 81;
    server_name 192.168.40.132;
    index index.html index.jsp index.htm;
    root /usr/local/nginx/html;
   
    access_log  /usr/local/nginx/logs/81-access.log main;
    error_log   /usr/local/nginx/logs/81-error.log;
  
location / 
  {
    proxy_pass http://192.168.40.129:81;
    proxy_redirect off ;
    
  }
}

9.3 cat 82.conf
访问132机器的81端口,反向代理到129机器的81端口

server 
{
    listen 82;
    server_name 192.168.40.132;
    index index.html index.jsp index.htm;
    root /usr/local/nginx/html;
   
    access_log  /usr/local/nginx/logs/82-access.log main;
    error_log   /usr/local/nginx/logs/82-error.log;
  
location / 
  {
    proxy_pass http://192.168.40.129:82;
    proxy_redirect off ;
    
  }
}

9.4
http://132:821/ex/ --> http://129:82/examples/

[root@xag132 vhosts]# cat 82-ex.conf 
server 
{
    listen 821;
    server_name 192.168.40.132;
    index index.html index.jsp index.htm;
    root /usr/local/nginx/html;
   
    access_log  /usr/local/nginx/logs/82-ex-access.log main;
    error_log   /usr/local/nginx/logs/82-ex-error.log;
  
location /ex/ 
  {
    proxy_pass http://192.168.40.129:82/examples/;
    proxy_redirect off ;
    
  }
}

9.5 cat slb80.conf 访问220机器的80端口myweb,负载均衡到后端的两台机器224和225的81端口myweb

upstream slb80 
{
    server 10.0.30.224:81 max_fails=3 fail_timeout=30s;   #max_fails = 3 为允许失败的次数,默认值为1
    server 10.0.30.225:81 max_fails=3 fail_timeout=30s;   #fail_timeout =30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间
}
server 
{
    listen 80;
    server_name 10.0.30.220;
    index index.html index.jsp index.htm;
    root /usr/local/nginx/html;
   
    access_log  /usr/local/nginx/logs/slb80-access.log main;
    error_log   /usr/local/nginx/logs/slb80-error.log;
  
location /myweb/ 
  {
    proxy_pass http://slb80/myweb/;
    proxy_redirect off ;
    
  }
}

9.6 cat slb443.conf

upstream slb443 
{
    server 192.168.40.129:81 max_fails=3 fail_timeout=30s;
    server 192.168.40.129:82 max_fails=3 fail_timeout=30s;
}
server 
{
    listen 443 ssl;
    server_name 192.168.40.132;
    index index.html index.jsp index.htm;
    root /usr/local/nginx/html;
   
    access_log  /usr/local/nginx/logs/slb443-access.log main;
    error_log   /usr/local/nginx/logs/slb443-error.log;
  
    ssl_certificate      /usr/local/nginx/cer/web132.crt;
    ssl_certificate_key  /usr/local/nginx/cer/web132.key;
    
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers  on;

 location /ex/ 
 {
    proxy_pass http://slb443/examples/;
    proxy_redirect off ;
    
 }
}

9.5 cat slb80.conf http 瀏覽器訪問 自動跳轉 https

server 
{
        listen       80;
        server_name  10.0.30.221;

        access_log  /usr/local/nginx/logs/slb80-access.log main;
        error_log   /usr/local/nginx/logs/slb80-error.log;

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

        return      301 https://$server_name$request_uri; 
}

级别越高,记录的信息越少,生产场景一般是warn|error|crit这三个级别之一

级别 级 别 值 级 别 说 明
debug 8 代码中标记为 NGX_LOG_DEBUG 的输出,输出最为详细,配合调试使用
info 7 代码中标记为 NGX_LOG_INFO 的输出,因包括除 debug 级别的所有输出,故同样会消耗大量磁盘 IO 资源
notice 6 代码中标记为 NGX_LOG_NOTICE 的输出
warn 5 代码中标记为 NGX_LOG_WARN 的输出
error 4 代码中标记为 NGX_LOG_ERROR 的输出,[生产推荐]
crit 3 代码中标记为 NGX_LOG_CRIT 的输出
alert 2 代码中标记为 NGX_LOG_ALERT 的输出
emerg 1 代码中标记为 NGX_LOG_EMERG 的输出
上一篇下一篇

猜你喜欢

热点阅读