运维|devops|nginx|基本配置拆解

📑 目录

1. 运维|devops|nginx|基本配置拆解

1.1 最小单元

1.1.1 http模块

1.1.2 server模块

1.1.3 location模块

1.2 简单配置

1.3 反向代理

1.4 include模块化拆分

1.5 proxy_pass

1.5.1 基础规则表(请求 /api/users,location /api/)

1.5.2 完整组合样例表(覆盖 5 种实战场景)

1.6 综合样例

最小单元

text

http {                      # 第1层:HTTP 全局配置
    server {                # 第2层:虚拟主机(站点)
        listen 80;
        server_name example.com;
        location / {        # 第3层:URL 路由
            root /var/www;  # 第4层: 静态页面地址
        }
    }
}

img

http模块

所有HTTP相关配置的最外层容器

text

一个nginx.conf只能有一个http块可定义多个server

server模块

定义一个虚拟主机(站点),通过listen和server_name区分不同的域名或端口

text

listen:监听的 IP 和端口(如 80、443 ssl)
server_name:匹配的域名(支持通配符和正则)
root / index:静态文件根目录和默认首页
ssl_certificate / ssl_certificate_key:SSL 证书路径

location模块

根据请求 URI 进行路由匹配,执行相应处理(如提供静态文件、转发到后端、重定向等)
  • 匹配规则

text

=:精确匹配,优先级最高
^~:前缀匹配,若匹配则不再检查正则
~(区分大小写)或 ~*(不区分)正则匹配
  • 常见处理

text

root / alias:指定文件路径
proxy_pass:转发请求到后端服务器
try_files:尝试多个文件/路径

简单配置

text

server {
    listen 80;
    server_name static.example.com;
    root /var/www/html;          # 所有 location 默认的根目录
    index index.html;            # 默认首页文件

    location /images/ {
        root /data;              # 实际路径为 /data/images/
    }

    location /css/ {
        alias /app/static/css/;  # alias 替换路径,实际为 /app/static/css/
    }
}

反向代理

  • 正向代理
(你平时用的 VPN/梯子),代表客户端你想访问谷歌,但直连被堵,于是你通过代理(VPN)出去,服务端(谷歌)不知道是谁在访问,只知道代理在访问(隐藏客户端)
  • 反向代理
反向代理(你现在的 Nginx),代表服务端,用户访问你的域名,但实际处理业务的是内网的 192.168.0.26:8085,客户端(用户)不知道背后有那么多服务器,只知道自己访问了 Nginx(隐藏服务端)

text

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

    location / {
        proxy_pass http://backend_server;   # 转发到 upstream 或直接写 应用服务器IP
        proxy_set_header Host $host;        # 传递原始 Host 头
        proxy_set_header X-Real-IP $remote_addr;
    }
}

include模块化拆分

把大配置文件拆成多个小段文件便于维护,nginx.conf仅仅维护通用和全局的

proxy_pass

基础规则表(请求 /api/userslocation /api/

配置写法Nginx 转发给后端的路径效果说明
proxy_pass http://backend:8080;(无斜杠)/api/users保留前缀(原样传递)
proxy_pass http://backend:8080/;(有斜杠)/users截断前缀(替换为 /

完整组合样例表(覆盖 5 种实战场景)

场景location 写法proxy_pass 写法请求示例转发给后端的实际 URI
标准保留前缀location /api/ {proxy_pass http://backend:8080;(无斜杠)/api/users/api/users
标准截断前缀location /api/ {proxy_pass http://backend:8080/;(有斜杠)/api/users/users
location 不带斜杠(保留)location /api {proxy_pass http://backend:8080;(无斜杠)/api/users/api/users
location 不带斜杠(截断)location /api {proxy_pass http://backend:8080/;(有斜杠)/api/users/users
路径替换(版本号重写)location /old_api/ {proxy_pass http://backend:8080/v2/;(带路径)/old_api/users/v2/users

综合样例

text

# /usr/local/nginx/conf/nginx.conf
user  nginx;
worker_processes  auto;
error_log  /var/log/nginx/error.log  warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    # 静态站点
    server {
        listen 80;
        server_name static.example.com;
        root /var/www/static;
        index index.html;
    }

    # 反向代理 + SSL
    server {
        listen 443 ssl;
        server_name api.example.com;

        ssl_certificate /etc/ssl/certs/api.pem;
        ssl_certificate_key /etc/ssl/private/api.key;

        location /api/ {
            proxy_pass http://backend:8080/;   # 去掉 /api 前缀
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }

        location /admin/ {
            proxy_pass http://backend:8080;    # 保留 /admin 前缀
            proxy_set_header Host $host;
        }
    }

    # HTTP 重定向到 HTTPS
    server {
        listen 80;
        server_name api.example.com;
        return 301 https://$host$request_uri;
    }
}

更多推荐