Nginx常用配置、反向代理

目录

[1. 常用配置](#1. 常用配置)

基本设置

HTTP配置

虚拟主机配置

[2. 高级配置](#2. 高级配置)

反向代理配置

SSL/TLS配置

负载均衡配置

1. 常用配置

基本设置
javascript 复制代码
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
  • user nginx;: 指定Nginx worker进程的运行用户。
  • worker_processes auto;: 自动设置worker进程的数量。
  • error_log /var/log/nginx/error.log warn;: 错误日志的路径和日志级别。
  • pid /var/run/nginx.pid;: Nginx主进程的PID文件路径。
HTTP配置
javascript 复制代码
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    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  /var/log/nginx/access.log  main;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    keepalive_timeout  65;
    types_hash_max_size 2048;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
  • include /etc/nginx/mime.types;: 引入MIME类型定义文件。
  • default_type application/octet-stream;: 默认的MIME类型。
  • log_format main ...: 定义日志格式。
  • access_log /var/log/nginx/access.log main;: 访问日志的文件路径。
  • sendfile on;: 开启sendfile功能。
  • tcp_nopush on; tcp_nodelay on;: 优化TCP传输。
  • keepalive_timeout 65;: 客户端与服务器之间的持久连接超时时间。
  • types_hash_max_size 2048;: MIME类型哈希表的最大大小。
虚拟主机配置
javascript 复制代码
server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/example.com;
        index index.html index.htm;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
  • server { ... }: 定义一个虚拟主机配置块。
  • listen 80;: 监听80端口。
  • server_name example.com;: 定义服务器的域名。
  • location / { ... }: 处理根路径请求。
  • error_page ...: 定义错误页面处理方式。

2. 高级配置

反向代理配置
javascript 复制代码
server {
    listen 80;
    server_name backend.example.com;

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

upstream backend_servers {
    server backend1.example.com;
    server backend2.example.com;
}
  • server { ... }: 定义反向代理的虚拟主机配置。
  • listen 80;: 监听80端口。
  • server_name backend.example.com;: 定义服务器的域名。
  • location / { ... }: 配置反向代理。
  • proxy_pass http://backend_servers;: 将请求代理到后端服务器组。
  • upstream backend_servers { ... }: 定义后端服务器组。
SSL/TLS配置
javascript 复制代码
server {
    listen 443 ssl;
    server_name secure.example.com;

    ssl_certificate /etc/nginx/ssl/secure.example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/secure.example.com.key;

    location / {
        root /var/www/secure.example.com;
        index index.html index.htm;
    }
}
  • server { ... }: 定义HTTPS的虚拟主机配置。
  • listen 443 ssl;: 监听443端口并启用SSL。
  • ssl_certificate ...; ssl_certificate_key ...;: 指定SSL证书和私钥的路径。
负载均衡配置
javascript 复制代码
upstream backend_servers {
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
}

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

    location / {
        proxy_pass http://backend_servers;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
  • upstream backend_servers { ... }: 定义负载均衡的后端服务器组。
  • proxy_pass http://backend_servers;: 将请求代理到后端服务器组。
相关推荐
神梦流5 小时前
GE 引擎的非标准数据流处理:稀疏张量与自定义算子在图优化中的语义保持
linux·运维·服务器
兜兜转转了多少年6 小时前
从脚本到系统:2026 年 AI 代理驱动的 Shell 自动化
运维·人工智能·自动化
Lsir10110_6 小时前
【Linux】中断 —— 操作系统的运行基石
linux·运维·嵌入式硬件
Doro再努力7 小时前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
全栈工程师修炼指南7 小时前
Nginx | stream content 阶段:UDP 协议四层反向代理浅析与实践
运维·网络·网络协议·nginx·udp
Lsir10110_7 小时前
【Linux】进程信号(上半)
linux·运维·服务器
开开心心就好8 小时前
发票合并打印工具,多页布局设置实时预览
linux·运维·服务器·windows·pdf·harmonyos·1024程序员节
火车叼位8 小时前
脚本伪装:让 Python 与 Node.js 像原生 Shell 命令一样运行
运维·javascript·python
鹏北海8 小时前
micro-app 微前端项目部署指南
前端·nginx·微服务
迎仔9 小时前
06-存储设备运维进阶:算力中心的存储管家
运维