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;: 将请求代理到后端服务器组。
相关推荐
云动雨颤1 小时前
Linux卡在emergency mode怎么办?xfs_repair 命令轻松解决
linux·运维·服务器
亮子AI1 小时前
【Ubuntu】清理空间的几种方法
linux·运维·ubuntu
零基础的修炼2 小时前
Linux---进程信号
运维·服务器
Sopaco2 小时前
告别项目文档滞后:Litho(deepwiki-rs)在CI/CD中的自动化文档生成实践
运维·ci/cd·自动化
Maple_land3 小时前
Linux进程第五讲:PPID与bash的关联、fork系统调用的原理与实践操作(上)
linux·运维·服务器·centos·bash
NicolasCage4 小时前
解决苍穹外卖WebSocket连接失败的问题
nginx
风为你而吹4 小时前
【玩泰山派】4、制作ubuntu镜像-(6)使用鲁班猫的sdk去制作镜像
linux·运维·ubuntu
影子24014 小时前
Windows Server2016 服务器安装JDK,一直卡在“应用程序正在为首次使用作准备,请稍候” ,导致jdk安装失败解决方案
运维·服务器·windows·jdk
养生技术人4 小时前
Oracle OCP认证考试题目详解082系列第48题
运维·数据库·sql·oracle·database·开闭原则·ocp
_OP_CHEN5 小时前
Linux 系统编程:(一)从历史演进到 XShell 远程登录实操
linux·运维·服务器·centos·unix·xshell