Nginx+安装与配置

1. Nginx 安装

1.1 安装前准备

创建工作目录

1
mkdir nginx && cd nginx

安装必要工具

1
2
3
4
5
6
7
8
# 安装wget下载工具
yum -y install wget

# 安装构建工具
yum -y install automake autoconf libtool make

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

1.2 下载与编译安装

下载Nginx源码

1
2
3
4
5
6
# 从官网下载源码包
wget http://nginx.org/download/nginx-1.17.6.tar.gz

# 解压源码包
tar -zxvf nginx-1.17.6.tar.gz
cd nginx-1.17.6

配置编译选项

1
2
3
4
# 配置安装路径和模块
./configure --prefix=/software/nginx \
          --with-http_stub_status_module \
          --with-http_ssl_module

参数说明:

  • --prefix=/software/nginx: 指定安装目录

  • --with-http_stub_status_module: 启用状态监控模块

  • --with-http_ssl_module: 启用SSL/HTTPS支持

编译与安装

1
2
3
4
5
6
7
8
# 编译
make

# 安装
make install

# 进入Nginx可执行文件目录
cd /software/nginx/sbin

2. Nginx 配置

2.1 基本配置文件位置

1
2
3
4
5
# 主配置文件
/software/nginx/conf/nginx.conf

# 其他配置文件目录
/software/nginx/conf/

2.2 反向代理配置

基本语法

1
2
3
4
5
6
7
8
9
10
11
12
server {
   listen port [ssl];
   server_name host;

   # HTTPS配置(可选)
  [ssl_certificate pem_path;]
  [ssl_certificate_key key.path;]

   location / {
       proxy_pass 代理url;
  }
}

实际配置示例

1
2
3
4
5
6
7
8
9
10
11
server {
   listen 443 ssl;
   server_name example.cn;

   ssl_certificate ../pem/example.cn.pem;
   ssl_certificate_key ../pem/example.cn.key;

   location / {
       proxy_pass http://127.0.0.1:3001;
  }
}

2.3 上传文件大小限制

设置最大上传大小

1
2
3
4
5
6
http {
# 设置客户端最大请求体大小
client_max_body_size 100m;

# 其他配置...
}

说明:

  • Nginx默认最大上传文件大小为1MB

  • 该配置需要放在http块中

  • 单位可以是k(KB)、m(MB)、g(GB)


3. Nginx 服务管理

3.1 基本服务控制

启动Nginx

1
2
3
4
5
# 进入可执行文件目录
cd /software/nginx/sbin

# 启动服务
./nginx

停止Nginx服务

1
2
3
4
5
6
7
8
# 优雅停止(推荐)
./nginx -s quit

# 立即停止
./nginx -s stop

# 强制停止
kill -9 nginx

重启与重载

1
2
3
4
5
6
# 重新加载配置文件
./nginx -s reload

# 重启服务
./nginx -s stop
./nginx

3.2 进程管理

查看Nginx进程

1
2
# 查看nginx进程信息
ps -ef | grep nginx

使用进程ID控制

1
2
3
4
5
6
7
8
9
10
# 获取主进程PID后,可以使用kill命令

# 优雅停止
kill -QUIT 主进程号

# 快速停止
kill -TERM 主进程号

# 强制停止
kill -9 nginx

3.3 配置文件检查

1
2
3
4
5
# 检查配置文件语法
./nginx -t

# 检查指定配置文件
./nginx -t -c /path/to/nginx.conf

4. 实用配置示例

4.1 多站点配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 站点1
server {
listen 80;
server_name site1.com;

location / {
proxy_pass http://127.0.0.1:3001;
}
}

# 站点2
server {
listen 80;
server_name site2.com;

location / {
proxy_pass http://127.0.0.1:3002;
}
}

4.2 负载均衡配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
upstream backend {
server 127.0.0.1:3001;
server 127.0.0.1:3002;
server 127.0.0.1:3003;
}

server {
listen 80;
server_name example.com;

location / {
proxy_pass http://backend;
}
}

4.3 静态文件服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 80;
server_name static.example.com;
root /var/www/static;

location / {
try_files $uri $uri/ =404;
}

# 缓存配置
location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
expires 1y;
add_header Cache-Control "public, immutable";
}
}

4.4 HTTPS重定向

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# HTTP自动重定向到HTTPS
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}

# HTTPS配置
server {
listen 443 ssl;
server_name example.com;

ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/private.key;

location / {
proxy_pass http://127.0.0.1:3001;
}
}

4.5 API网关配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
listen 80;
server_name api.example.com;

# API版本1
location /v1/ {
proxy_pass http://127.0.0.1:3001/;
}

# API版本2
location /v2/ {
proxy_pass http://127.0.0.1:3002/;
}

# 健康检查
location /health {
access_log off;
return 200 "OK";
add_header Content-Type text/plain;
}
}

4.6 跨域配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name api.example.com;

location / {
# 允许跨域
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';

if ($request_method = 'OPTIONS') {
return 204;
}

proxy_pass http://127.0.0.1:3001;
}
}

5. 故障排查

5.1 常见问题

端口被占用

1
2
3
# 查看端口占用情况
netstat -tlnp | grep :80
lsof -i :80

权限问题

1
2
3
# 确保nginx用户有适当权限
chown -R nginx:nginx /var/log/nginx/
chown -R nginx:nginx /var/cache/nginx/

配置文件错误

1
2
3
4
5
# 检查配置语法
nginx -t

# 查看错误日志
tail -f /software/nginx/logs/error.log

5.2 性能优化

工作进程配置

1
2
3
# nginx.conf
worker_processes auto; # 自动根据CPU核心数设置
worker_connections 1024; # 每个进程最大连接数

缓存配置

1
2
3
4
5
# 开启gzip压缩
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

6. 资源链接