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;: 将请求代理到后端服务器组。
相关推荐
DY009J15 分钟前
深度探索Kali Linux的精髓与实践应用
linux·运维·服务器
什么鬼昵称1 小时前
Pikachu- Over Permission-垂直越权
运维·服务器
码农小白1 小时前
linux驱动:(22)中断节点和中断函数
linux·运维·服务器
4647的码农历程1 小时前
Linux网络编程 -- 网络基础
linux·运维·网络
醉颜凉2 小时前
银河麒麟桌面操作系统V10 SP1:取消安装应用的安全授权认证
运维·安全·操作系统·国产化·麒麟·kylin os·安全授权认证
C++忠实粉丝2 小时前
Linux环境基础开发工具使用(2)
linux·运维·服务器
康熙38bdc3 小时前
Linux 环境变量
linux·运维·服务器
存储服务专家StorageExpert3 小时前
DELL SC compellent存储的四种访问方式
运维·服务器·存储维护·emc存储
大G哥4 小时前
记一次K8S 环境应用nginx stable-alpine 解析内部域名失败排查思路
运维·nginx·云原生·容器·kubernetes
妍妍的宝贝4 小时前
k8s 中微服务之 MetailLB 搭配 ingress-nginx 实现七层负载
nginx·微服务·kubernetes