Nginx安装配置完整教程,从安装到运行

前言

刚装好 CentOS 服务器,想跑个网站,第一步就是装 Nginx。Nginx 是目前最流行的 Web 服务器,性能高、占用资源少,还能做反向代理和负载均衡。本文就从安装 Nginx 开始,一步步讲怎么配置一个能跑的网站。

第一步:安装 Nginx

1. 安装 EPEL 源

CentOS7 默认源里的 Nginx 版本比较老,先装 EPEL 源:

yum install epel-release -y

2. 安装 Nginx

yum install nginx -y

3. 启动 Nginx

systemctl start nginx

4. 设置开机自启

systemctl enable nginx

5. 开放防火墙

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

6. 测试

浏览器访问你的服务器 IP,应该能看到 Nginx 默认欢迎页面。

第二步:Nginx 配置文件结构

Nginx 的配置文件都在 /etc/nginx/ 目录下:

/etc/nginx/
├── nginx.conf          # 主配置文件
├── conf.d/             # 子配置文件目录
│   └── default.conf   # 默认站点配置
├── fastcgi.conf
├── mime.types
└── ...

一般我们写站点配置,都放在 conf.d/ 目录下,每个站点一个文件。

第三步:配置第一个站点

1. 创建网站根目录

mkdir -p /var/www/html/test

2. 写一个测试页面

echo "Hello Nginx" > /var/www/html/test/index.html

3. 创建站点配置文件

vi /etc/nginx/conf.d/test.conf

写入:

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

    root   /var/www/html/test;
    index  index.html index.htm;

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

4. 检查配置是否正确

nginx -t

输出 test is successful 就是没问题。

5. 重新加载 Nginx

systemctl reload nginx

6. 测试

浏览器访问 www.example.com,应该能看到 "Hello Nginx"。

注意: 要把域名解析到你服务器的 IP 上,或者在本地 hosts 文件里加一条记录。

常用配置说明

listen 监听端口

listen 80;

监听 80 端口,HTTP 默认端口。

如果要监听 HTTPS 的 443 端口:

listen 443 ssl;

server_name 域名

server_name www.example.com example.com;

可以写多个域名,用空格隔开。

root 网站根目录

root /var/www/html/test;

网站文件放在这个目录下。

index 默认首页

index index.html index.htm;

用户访问网站根目录的时候,默认找哪个文件。

配置 PHP 支持

如果要跑 PHP 网站,还需要配置 PHP-FPM。

1. 安装 PHP-FPM

yum install php-fpm php-mysql -y

2. 启动 PHP-FPM

systemctl start php-fpm
systemctl enable php-fpm

3. 修改 Nginx 配置

在站点配置里加上 PHP 支持:

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

    root   /var/www/html/test;
    index  index.php index.html index.htm;

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

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

4. 重启 Nginx

systemctl restart nginx

5. 测试 PHP

创建一个 info.php 文件:

echo "<?php phpinfo(); ?>" > /var/www/html/test/info.php

浏览器访问 www.example.com/info.php,应该能看到 PHP 信息页面。

常见坑

坑1:403 Forbidden

访问网站报 403,一般是权限问题。

排查:

  1. 看文件权限是不是 644,目录是不是 755
  2. 看所有者是不是 nginx
  3. 看 SELinux 是不是开着
chown -R nginx:nginx /var/www/html/test
chmod -R 755 /var/www/html/test

坑2:502 Bad Gateway

访问网站报 502,说明 Nginx 连不上 PHP-FPM。

排查:

  1. 看 PHP-FPM 有没有启动:systemctl status php-fpm
  2. 看配置里的 fastcgi_pass 地址对不对

坑3:配置改完不生效

改了 Nginx 配置,没 reload,结果配置没生效。

解决: 改完配置记得 reload:

nginx -t
systemctl reload nginx

坑4:域名解析了还是打不开

域名解析了,但还是打不开。

排查:

  1. 看防火墙有没有开 80 端口
  2. 看安全组有没有开 80 端口(云服务器)
  3. 看域名解析有没有生效:ping www.example.com

总结

Nginx 安装配置的完整流程:

  1. 安装 Nginx:yum install nginx -y
  2. 启动并开机自启:systemctl start nginx && systemctl enable nginx
  3. 开放防火墙:80 和 443 端口
  4. 写站点配置:在 conf.d/ 下创建配置文件
  5. 检查并重载:nginx -t && systemctl reload nginx

Nginx 是 Web 服务器的入门必备,装完这个就能跑网站了。

遇到问题加QQ23979811 协助处理


emer 发布于  2026-10-5 07:52 

Nginx 配置实战:反向代理、负载均衡与缓存

前言

Nginx 是目前最流行的 Web 服务器和反向代理软件。很多人会写简单的配置,但对反向代理、负载均衡、缓存这些高级用法并不熟悉。本文带你从零开始,掌握 Nginx 的核心配置技巧。

一、反向代理基础

反向代理是 Nginx 最常用的功能之一。用户请求 Nginx,Nginx 再把请求转发给后端应用服务器。

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

关键说明:

  • proxy_pass:后端服务地址
  • proxy_set_header:传递真实的客户端信息给后端,否则后端拿到的都是 Nginx 的信息

二、负载均衡

当后端有多台服务器时,可以用 Nginx 做负载均衡。

1. 轮询(默认)

upstream backend {
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
    server 127.0.0.1:8082;
}

server {
    listen 80;
    server_name example.com;

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

2. 加权轮询

upstream backend {
    server 127.0.0.1:8080 weight=3;
    server 127.0.0.1:8081 weight=2;
    server 127.0.0.1:8082 weight=1;
}

weight 值越大,分配的请求越多。适合配置不同的服务器。

3. ip_hash 会话保持

upstream backend {
    ip_hash;
    server 127.0.0.1:8080;
    server 127.0.0.1:8081;
}

同一个 IP 的请求总是分配到同一台后端服务器,适合有 session 的场景。

三、缓存配置

Nginx 缓存可以大幅减轻后端压力,提升响应速度。

1. 配置缓存路径

http {
    # 定义缓存路径和参数
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;

    server {
        listen 80;
        server_name example.com;

        location / {
            proxy_pass http://backend;
            proxy_cache my_cache;
            proxy_cache_valid 200 302 10m;
            proxy_cache_valid 404 1m;
            proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
            add_header X-Cache-Status $upstream_cache_status;
        }
    }
}

关键参数说明:

  • keys_zone:缓存共享内存区大小
  • max_size:缓存最大占用空间
  • inactive:多久没访问就自动清理
  • proxy_cache_valid:不同状态码的缓存时间
  • X-Cache-Status:查看是否命中缓存(HIT/MISS/BYPASS)

2. 不缓存特定请求

location /admin/ {
    proxy_pass http://backend;
    proxy_cache off;  # 后台页面不缓存
}

四、HTTPS 配置

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/key.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;

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

# HTTP 跳转 HTTPS
server {
    listen 80;
    server_name example.com;
    return 301 https://$host$request_uri;
}

五、常见问题排查

1. 502 Bad Gateway

通常是后端服务挂了或者端口不对:

# 检查后端服务是否在运行
netstat -tlnp | grep 8080

# 查看 Nginx 错误日志
tail -f /var/log/nginx/error.log

2. 上传文件大小限制

http {
    client_max_body_size 50M;
}

3. 超时设置

location / {
    proxy_connect_timeout 60s;
    proxy_read_timeout 60s;
    proxy_send_timeout 60s;
}

总结

Nginx 配置的核心思路:

  1. 反向代理:把请求转发给后端应用
  2. 负载均衡:多台后端服务器分担流量
  3. 缓存:静态内容缓存,减少后端压力
  4. HTTPS:加密传输,安全第一

掌握这些配置,你就能应对大部分 Web 服务器场景了。Nginx 配置文件改完记得执行 nginx -t 检查语法,然后 nginx -s reload 重载生效。


emer 发布于  2026-10-4 09:56