CentOS搭建:Nginx

12/31/2023 Linux

目录


参考:


# CentOS搭建:Nginx

sudo:Linux系统管理指令,允许系统管理员让普通用户执行部分root命令,如 halt、reboot、su 等。

# 环境依赖

软件/环境 版本 包名 备注
Linux CentOS 7.x
Nginx 1.24.0 nginx-1.24.0.tar.gz (opens new window) 官网 (opens new window)

# 安装依赖

# sudo yum -y update
sudo yum -y install gcc gcc-c++ make 
sudo yum -y install libtool     
sudo yum -y install zlib zlib-devel
sudo yum -y install openssl openssl-devel
sudo yum -y install pcre pcre-devel  
1
2
3
4
5
6

# 解压缩包

sudo tar -zxvf nginx-1.24.0.tar.gz -C /soft/nginx
1

# 编译&安装

cd /soft/nginx/nginx-1.24.0
# 安装配置
sudo ./configure --prefix=/soft/nginx/nginx-1.24.0/local/nginx --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module
# 编译&安装
sudo make && sudo make install
1
2
3
4
5

configure命令是用来配置Nginx的编译选项,包括:

  • --prefix=/soft/nginx/nginx-1.24.0/local/nginx:指定Nginx安装后的根目录。所有的Nginx文件(包括可执行文件、配置文件等)都将安装在这里。
  • --with-http_stub_status_module:启用Nginx的Stub Status模块,这个模块用于提供Nginx服务器的状态信息。
  • --with-http_v2_module:启用HTTP/2模块,这个模块用于支持HTTP/2协议。
  • --with-http_ssl_module:启用SSL模块,这个模块用于支持HTTPS协议。
  • --with-http_realip_module:启用Real IP模块,这个模块用于从HTTP请求头(如X-Forwarded-For)中获取真实客户端IP地址。
  • --with-http_sub_module:启用替换模块,这个模块允许在响应内容中替换文本。
  • --with-http_gzip_static_module:启用Gzip静态模块,这个模块用于预压缩文件,并在用户请求时发送压缩后的文件,以减少网络传输量。
  • --with-stream 选项用于启用 Nginx 的 TCP/UDP 代理模块(stream 模块)。这个模块允许 Nginx 处理非 HTTP 协议的 TCP 和 UDP 流。例如,你可以使用它来代理 SSL/TLS 加密的 TCP 连接(如 HTTPS 流量,但更常用于其他协议如 MySQL、MongoDB、Redis 等的加密连接),或者简单的 TCP/UDP 转发。启用 stream 模块后,你可以在 Nginx 配置文件中使用 stream 块来定义这些代理规则,类似于 HTTP 服务器的 server 块。
  • --with-stream_ssl_module 选项用于在 Nginx 的 stream 模块中启用 SSL/TLS 支持。这意味着你可以使用 Nginx 来代理加密的 TCP 连接,如 HTTPS 流量(尽管这通常通过 HTTP 模块处理)或其他基于 SSL/TLS 的协议。这个模块允许你在 stream 配置块中使用 SSL/TLS 相关的指令,如 ssl_certificatessl_certificate_key,来配置 SSL/TLS 证书和密钥,从而保护 TCP 连接的安全性。

# 修改配置

ll -a /soft/nginx/nginx-1.24.0/local/nginx/conf/ |grep nginx
# 备份
sudo cp /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf.bak20240703
ll -a /soft/nginx/nginx-1.24.0/local/nginx/conf/ |grep nginx
1
2
3
4
# 创建附加配置文件的目录
sudo mkdir -p /soft/nginx/nginx-1.24.0/local/nginx/conf/conf.d
	# 引入附件配置文件:在default_type行上插入include conf.d/*.conf;
sudo sed -i '/default_type/i include conf.d/*.conf;' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
	# 定义运行Nginx工作进程的用户:在#user行下插入user root;
sudo sed -i '/#user/a user root;' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
	# 开启错误日志:在0到#error_log所在行,替换#error_log为error_log
sudo sed -i '0,/#error_log/s/#error_log/error_log/' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
	# 开启访问日志:在0到#access_log所在行,替换#access_log为access_log
sudo sed -i '0,/#access_log/s/#access_log/access_log/' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
	# 定义日志格式:在23、24、25行,替换所有的#为''
sudo sed -i '23s/#//g' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
sudo sed -i '24s/#//g' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
sudo sed -i '25s/#//g' /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14

使用nginx -t命令来测试配置文件的语法是否正确

# 测试配置文件语法是否正确
sudo /soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx -t
1
2

# 配置服务

创建或编辑systemd的服务单元文件,该文件描述了如何启动、停止和管理Nginx服务。

sudo tee /etc/systemd/system/nginx.service <<EOF
[Unit]
# 服务的简短描述
Description=nginx 
# 服务启动的依赖条件:在网络服务启动之后启动
After=network.target 
   
[Service]
# 服务的启动类型,forking:作为子进程启动
Type=forking
# 指定服务主进程的PID文件位置
PIDFile=/soft/nginx/nginx-1.24.0/local/nginx/logs/nginx.pid
# 在启动服务之前执行的命令,这里用于测试配置文件的正确性
ExecStartPre=/soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx -t -c /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf
# 启动服务的命令
ExecStart=/soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx
# 重新加载服务的命令,这里用于在不中断服务的情况下重新加载配置
ExecReload=/soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx -s reload
# 停止服务的命令
ExecStop=/soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx -s stop
# 服务将使用自己的临时目录,而不是系统共享的临时目录
PrivateTmp=true

[Install]
# 指定服务安装后的默认运行级别,multi-user.target是大多数服务器和桌面系统使用的级别,表示多用户模式
WantedBy=multi-user.target
EOF
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

# 服务管理

# 重新加载systemd的守护进程(daemon)配置
sudo systemctl daemon-reload
# 配置系统启动时启用nginx服务
sudo systemctl enable nginx
1
2
3
4
# 服务启动
sudo systemctl start nginx
# 服务状态
sudo systemctl status nginx
# 服务停止
sudo systemctl stop nginx
# 服务重启
sudo systemctl restart nginx
1
2
3
4
5
6
7
8

# 常用命令

sudo /soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx -t
1

# 快捷命令

  • 测试Nginx配置文件
# 创建环境变量文件
sudo vi /etc/profie.d/nginx.sh
1
2
# 添加测试Nginx快捷命令
alias tnc='sudo /soft/nginx/nginx-1.24.0/local/nginx/sbin/nginx -t -c /soft/nginx/nginx-1.24.0/local/nginx/conf/nginx.conf'
1
2
# 生效
source /etc/profile.d/nginx.sh
1
2
# 使用
tnc
1
2

image-20241023104732808

# 反向代理配置

nginx反向代理location和proxy_pass斜杠 (opens new window)

http {  
    ...  
    server {  
        listen 80;  # 监听80端口,HTTP协议的默认端口  
        server_name your_domain_name_or_ip;  # 替换为你的域名或IP地址  
  
        location / {  
            # 当请求的路径匹配时,执行反向代理  
            proxy_pass http://localhost:8080/;  # 指定代理的目标地址  
            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头部,记录客户端的IP地址链  
            # 可以根据需要添加更多的proxy_set_header指令  
        }  
  
        # 可以根据需要添加更多的location块来定义不同的代理规则  
        ...  
    }  
  
    # 可以根据需要添加更多的server块来定义不同的服务  
    ...  
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

在 Nginx 的反向代理配置中,proxy_set_header 指令用于设置向后端服务器传递的请求头。

这些请求头对于后端服务器了解请求的原始来源、客户端 IP 地址、端口号等信息非常重要。

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-Port $remote_port;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
1
2
3
4
5

以下是对您提供的 proxy_set_header 配置指令的解释:

  1. proxy_set_header Host $http_host;

    设置请求头Host 的值为 $http_host 变量的值。

    $http_host 变量包含了请求行中的主机名和端口号(如果请求中包含了端口号)。

    用于确保后端服务器能够接收到与原始请求相同的 Host 字段值。

  2. proxy_set_header X-Real-IP $remote_addr;

    设置请求头X-Real-IP 的值为 $remote_addr 变量的值。

    $remote_addr 变量包含了客户端的 IP 地址。

    用于确保后端服务器能够接收到客户端的真实 IP 地址。

  3. proxy_set_header X-Real-Port $remote_port;

    设置请求头X-Real-Port 的值为 $remote_port 变量的值。

    Nginx 默认并没有 $remote_port 这个变量。通常,$remote_port 是在某些特定情况下(如使用 Stream 模块处理 TCP/UDP 流量时)才可用的。

    用于确保后端服务器能够接收到客户端的真实端口号。

  4. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    设置请求头X-Forwarded-For 的值为 $proxy_add_x_forwarded_for 变量的值。

    $proxy_add_x_forwarded_for 变量会包含原始请求中已有的 X-Forwarded-For 头的值(如果有的话),并在其后追加当前客户端的 IP 地址。

    用于记录经过的代理服务器的 IP 地址链。

  5. proxy_set_header X-Forwarded-Proto $scheme;

    设置请求头X-Forwarded-Proto$scheme变量的值。

    $scheme变量包含了请求的协议,即httphttps

    用于确保后端服务器能够识别请求是通过HTTP还是HTTPS发送的。

上次更新时间: 2/11/2025, 2:05:49 AM