中间件

Nginx 基础

·9 分钟阅读·3469 字

Nginx Web 服务器——安装配置、虚拟主机、Location、反向代理、负载均衡、HTTPS、PHP 解析

📋 目录

2. 配置文件结构

核心文件

配置文件用途
nginx.conf主配置文件
conf.d/*.conf子配置文件

nginx.conf 基本结构


worker_processes  1;               # 全局块

events {

    worker_connections  1024;       # events 块

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    upstream backend {              # 后端服务器组

        server 192.168.1.10:8080;

    }

    server {                        # server 块(虚拟主机)

        listen       80;

        server_name  localhost;

        location / {                # location 块

            root   html;

            index  index.html;

        }

    }

}

3. 基础命令


nginx                # 启动

nginx -s stop        # 停止

nginx -s reload      # 重载配置(不中断服务)

nginx -t             # 检查配置语法

nginx -V             # 查看版本与编译参数

4. 虚拟主机

类型示例说明
基于端口listen 81不同端口不同站点
基于 IPlisten 192.168.1.1:80多网卡
基于域名server_name www.a.com最常用

server {

    listen       80;

    server_name  www.a.com;

    location / {

        root   /var/www/a;

        index  index.html;

    }

}

server {

    listen       80;

    server_name  www.b.com;

    location / {

        root   /var/www/b;

        index  index.html;

    }

}

5. Location 匹配规则

匹配优先级


优先级从高到低:

1.  =       精确匹配         location = /abc

2.  ^~      前缀优先匹配      location ^~ /static/

3.  ~/~*    正则匹配          location ~ \.php$

4.  无前缀  普通前缀匹配      location /abc

root 与 alias 区别


# root:URI 追加到目录后

location /images/ {

    root /var/www/html;

}

# 请求 /images/logo.png → /var/www/html/images/logo.png

# alias:替换匹配路径

location /images/ {

    alias /var/data/photos/;

}

# 请求 /images/logo.png → /var/data/photos/logo.png

6. 反向代理


server {

    listen 80;

    server_name api.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_connect_timeout连接超时
proxy_read_timeout读取超时
proxy_buffer_size响应头缓冲区

7. 负载均衡


upstream backend {

    # 加权轮询

    server 192.168.1.10:8080 weight=3;

    server 192.168.1.11:8080 weight=2;

    server 192.168.1.12:8080 weight=1;

}

server {

    listen 80;

    location / {

        proxy_pass http://backend;

    }

}

算法:

算法说明
round-robin(默认)轮询
weight加权轮询
ip_hashIP 哈希(Session 保持)
least_conn最小连接数

健康检查:


upstream backend {

    server 192.168.1.10:8080 max_fails=3 fail_timeout=30s;

    server 192.168.1.11:8080 max_fails=3 fail_timeout=30s;

}
  • max_fails=3:连续失败 3 次认为不可用
  • fail_timeout=30s:30 秒内不再转发

8. HTTPS


server {

    listen 443 ssl;

    server_name example.com;

    ssl_certificate     /etc/nginx/ssl/example.com.pem;

    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    ssl_protocols       TLSv1.2 TLSv1.3;

    ssl_ciphers         HIGH:!aNULL:!MD5;

    location / {

        root   /var/www/html;

        index  index.html;

    }

}

# HTTP 跳转 HTTPS

server {

    listen 80;

    server_name example.com;

    return 301 https://$host$request_uri;

}

九、PHP 解析(FastCGI)


server {

    listen 80;

    root  /var/www/html;

    location / {

        index index.php index.html;

    }

    location ~ \.php$ {

        fastcgi_pass unix:/var/run/php-fpm/www.sock;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

        include        fastcgi_params;

    }

}
通信方式优点
Unix Socket更快更安全,仅本地
TCP Socket(127.0.0.1:9000)可跨主机

十、与 Tomcat 配合


upstream tomcat {

    server 127.0.0.1:8080;

    server 127.0.0.1:8081;

}

server {

    listen 80;

    server_name app.example.com;

    # 静态资源 Nginx 直接处理

    location /static/ {

        root /var/www/app;

        expires 7d;

    }

    # 动态请求转发 Tomcat

    location / {

        proxy_pass http://tomcat;

        proxy_set_header Host $host;

        proxy_set_header X-Real-IP $remote_addr;

    }

}

十一、配置速查

场景配置
静态缓存expires 7d;
上传限制client_max_body_size 10m;
禁止敏感文件location ~ /\. { deny all; }
URL 重写rewrite ^/old$ /new permanent;
配置验证nginx -t
重载配置nginx -s reload

关联文档

Yanche Blog

记录云原生、Linux、数据库等技术领域的学习心得,以及日常生活的思考与感悟。

© 2026 Yanche Blog. All rights reserved.

Powered by Astro