Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

linux nginx 配置文件

作者:翻成云恨雨愁   发布日期:2026-07-10   浏览:53

# nginx 配置文件示例

# 定义 Nginx 运行的用户和用户组
user  nginx;
# 工作进程数,一般设置为CPU核心数
worker_processes  auto;

# 错误日志定义
error_log  /var/log/nginx/error.log warn;
# 进程PID文件
pid        /var/run/nginx.pid;

# 工作模式及连接数上限
events {
    # 使用epoll事件模型,Linux下性能较好
    use   epoll;
    # 单个进程最大连接数(最大客户端连接数=worker_processes * worker_connections)
    worker_connections  1024;
}

# 设置HTTP服务器,利用它的反向代理功能
http {
    # 引入mime类型配置文件
    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;

    # 长连接超时时间
    keepalive_timeout  65;

    # Gzip 压缩模块设置
    gzip  on;
    gzip_disable "msie6";

    # 负载均衡配置,后端服务器列表
    upstream backend {
        server backend1.example.com weight=5;
        server backend2.example.com weight=5;
        server backend3.example.com backup;
    }

    # 服务器数组
    server {
        # 监听80端口
        listen       80;
        # 域名可以有多个,用空格隔开
        server_name  example.com www.example.com;

        # 根目录和默认首页
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        # 反向代理的设置
        location /api/ {
            proxy_pass http://backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
        }

        # 错误页面的设置
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    }
}

解释说明:

  1. 全局块:配置影响整个Nginx的行为,如运行用户、工作进程数等。
  2. events块:配置Nginx的工作模式与连接数上限。
  3. http块:配置HTTP服务器相关参数,包括MIME类型、日志格式、Gzip压缩等。
  4. server块:配置虚拟主机,监听端口、域名、根目录等。
  5. location块:配置URL匹配规则,处理静态文件或反向代理请求。
  6. upstream块:配置负载均衡,定义后端服务器列表。

通过以上配置,Nginx可以实现静态文件服务、反向代理、负载均衡等功能。

上一篇:linux scp用法

下一篇:linux find用法

大家都在看

linux如何启动nginx

linux常用命令查询端口是否正常

linux 发送邮件

linux长ping命令

linux groupadd

linux关机命令行

linux 安装 gcc

linux重启oracle命令

linux把一个文件夹移动到另一个文件夹里

linux查看系统运行时间

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站