安装与配置 Nginx

安装依赖

> yum install -y gcc gcc-c++ make
> yum install -y pcre pcre-devel
> yum install -y zlib zlib-devel
> yum install -y openssl openssl-devel

下载并解压

> wget https://nginx.org/download/nginx-1.24.0.tar.gz
> tar -zxvf nginx-1.24.0.tar.gz
> cd nginx-1.24.0

编译安装

> ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_gzip_static_module
> make
> make install

常用编译参数:

  • --prefix:安装目录,默认 /usr/local/nginx
  • --with-http_ssl_module:支持 HTTPS
  • --with-http_gzip_static_module:支持预压缩的静态文件

查看安装位置:

> whereis nginx
nginx: /usr/local/nginx

安装后的目录结构:

路径说明
/usr/local/nginx/conf/nginx.conf主配置文件
/usr/local/nginx/html默认站点目录
/usr/local/nginx/logs日志与 pid 文件
/usr/local/nginx/sbin/nginx可执行文件

启动与常用命令

> /usr/local/nginx/sbin/nginx            # 启动
> /usr/local/nginx/sbin/nginx -t         # 检查配置语法
> /usr/local/nginx/sbin/nginx -s reload  # 重载配置
> /usr/local/nginx/sbin/nginx -s quit    # 平滑停止
> /usr/local/nginx/sbin/nginx -s stop    # 立即停止
> /usr/local/nginx/sbin/nginx -v         # 查看版本
> /usr/local/nginx/sbin/nginx -V         # 查看版本与编译参数

验证是否启动成功:

> curl -I http://127.0.0.1
HTTP/1.1 200 OK

为方便使用,可以创建软链接:

> ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

配置 systemd 服务

新建:

/usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/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 quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启用服务:

> systemctl daemon-reload
> systemctl enable nginx
> systemctl start nginx
> systemctl status nginx

常见配置

静态站点

server {
    listen       80;
    server_name  www.example.com;

    location / {
        root   /data/www/web;
        index  index.html index.htm;
    }
}

反向代理

server {
    listen       80;
    server_name  api.example.com;

    location / {
        proxy_pass       http://127.0.0.1:5000;
        proxy_set_header Host              $host;
        proxy_set_header X-Real-IP         $remote_addr;
        proxy_set_header X-Forwarded-For   $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

开启 gzip

gzip on;
gzip_min_length 1k;
gzip_comp_level 5;
gzip_types text/plain text/css application/json application/javascript;

访问日志

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;

修改配置后先执行 nginx -t 校验,再执行 nginx -s reload 生效。

常见问题

  • bind() to 0.0.0.0:80 failed (98: Address already in use):80 端口被占用,用 ss -tunlp | grep :80 查看占用进程。
  • 配置修改后不生效:确认已执行 nginx -s reload,并检查 nginx -t 是否报错。
  • 访问返回 403:检查站点目录权限,以及 nginx 运行用户(默认 nobody)是否有读权限。
  • open() "/usr/local/nginx/logs/nginx.pid" failed:nginx 未启动或 pid 文件丢失,直接启动即可。
  • 源码安装在 systemctl start nginx 时报 Unit nginx.service not found:确认 unit 文件路径正确,并执行过 systemctl daemon-reload。

参考