Nginx:安装与站点配置

参考文档

  1. Nginx 安装配置详解 - CSDN
  2. Nginx 编译安装教程 - 博客园

环境准备(使用代理安装)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 设置代理
export http_proxy=http://172.200.96.179:3128

# 安装基础工具
yum install xinetd telnet telnet-server -y
yum install vim -y
yum install wget -y

# 安装编译依赖
yum install gcc -y
yum install pcre pcre-devel -y
yum install zlib zlib-devel -y
yum install openssl openssl-devel -y

# 下载 Nginx 源码
wget http://nginx.org/download/nginx-1.22.1.tar.gz

编译安装步骤

1
2
3
4
5
6
7
8
9
# 解压源码包
tar zxvf nginx-1.22.1.tar.gz

# 配置安装路径
cd nginx-1.22.1
./configure --prefix=/usr/local/nginx

# 编译并安装
make && make install

Nginx 基本操作

1
2
3
4
5
6
7
8
# 启动 Nginx
/usr/local/nginx/sbin/nginx

# 停止 Nginx
/usr/local/nginx/sbin/nginx -s stop

# 重启 Nginx
/usr/local/nginx/sbin/nginx -s reload

Systemctl 服务配置

  1. 创建服务文件:
1
vim /usr/lib/systemd/system/nginx.service
  1. 服务文件内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target
  1. 启用服务:
1
2
3
4
5
6
7
8
chmod +x /usr/lib/systemd/system/nginx.service
systemctl daemon-reload

# 服务管理命令
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx

Nginx 主配置示例 (nginx.conf)

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
user  nginx;
worker_processes auto;

error_log /usr/local/nginx/logs/error.log notice;
pid /usr/local/nginx/nginx.pid;

events {
worker_connections 1024;
}

http {
include /usr/local/nginx/conf/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 /usr/local/nginx/logs/access.log main;
sendfile on;
keepalive_timeout 65;

# 包含子配置
include /usr/local/nginx/conf.d/*.conf;

# 上游服务器配置
upstream nginx.kubezt.com {
server 192.168.38.101:39080 weight=2;
server 192.168.39.19:39080 weight=2;
}

upstream nginx.my-server.com {
server 192.168.38.101:39080;
}

upstream my-server1 {
server 192.168.38.101:39080; # zt的ingress
server 192.168.38.101:38181; # zt的zuul网关
}

# 服务端配置
server {
listen 3030;
server_name localhost;

# 注意:代理地址必须与 ingress 的 host 地址匹配
location /zttest {
proxy_pass http://nginx.my-server.com/;
}

location /ztdomain {
proxy_pass http://nginx.kubezt.com/;
}

location /zt1 {
proxy_pass http://my-server1/;
}
}
}

配置要点说明

  1. 代理规则
    • 必须使用与 Ingress 完全匹配的 host 地址(如 nginx.kubezt.com
    • 直接使用 IP+端口 会导致 404 错误
  2. 路径匹配
    • 转发到单独服务可行,但转发到 Ingress 需要特殊配置
  3. 包含机制
    • 使用 include /usr/local/nginx/conf.d/*.conf 加载模块化配置
  4. 负载均衡
    • 通过 upstream 模块配置多后端服务器
    • 支持权重分配(weight 参数)