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;: 将请求代理到后端服务器组。
相关推荐
(:满天星:)2 小时前
第31篇:块设备与字符设备管理深度解析(基于OpenEuler 24.03)
linux·运维·服务器·网络·centos
小陶来咯2 小时前
【仿muduo库实现并发服务器】Acceptor模块
运维·服务器
cui_hao_nan2 小时前
Docker后端部署
运维·docker·容器
ZZH1120KQ3 小时前
Linux系统安全及应用
linux·运维·系统安全
小扎仙森3 小时前
关于服务器宝塔转移wordperss子比主题问题
运维·服务器
小小小糖果人3 小时前
Linux云计算基础篇(5)
linux·运维·服务器
我不是哆啦A梦3 小时前
破解风电运维“百模大战”困局,机械版ChatGPT诞生?
运维·人工智能·python·算法·chatgpt
weixin_7714323114 小时前
linux系统 weblogic10.3.6(jar) 下载及安装
linux·运维·jar
绝不偷吃4 小时前
FastDFS分布式储存
linux·nginx
scuter_yu4 小时前
主流零信任安全产品深度介绍
运维·网络·安全