Nginx 常用配置

Nginx 常用配置



一、常用反向代理配置

    server {
        listen 8899;                #监听端口
        server_name  _;

        location /api/ {
            # 这里配置代理到后端服务的地址
            proxy_pass http://127.0.0.1:19009/;
        }

        location / {
            # 这里配置前端资源的路径
            root /usr/local/project/invoice/dist;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }

二、图片代理配置

    server {
        listen 8848;
        server_name _;

        location /mac {
            limit_req zone=mylimit burst=5;
            # 这里配置前端资源的路径
            alias /usr/local/project/mac-fxb/images/;

        client_max_body_size 20M; # 设置请求实体最大大小为20MB
        }


        location /macPro {
            limit_req zone=mylimit burst=5;
            # 这里配置前端资源的路径
            alias /usr/local/project/mac-fxb/imagesPro/;

        client_max_body_size 20M; # 设置请求实体最大大小为20MB
        }


        location / {
            limit_req zone=mylimit burst=5;
            # 这里配置前端资源的路径
            alias /usr/local/project/invoice/images/;

        client_max_body_size 20M; # 设置请求实体最大大小为20MB
        }



		# 定义一个用于返回 403 Forbidden 错误的 location
        error_page 403 /403.html;
        location = /403.html {
            root /etc/nginx/;
            internal; # 表示这个 location 只能内部重定向,不能直接访问
        }
    }

三、长连接代理配置

    server {
        listen 19008;                # 监听端口
        server_name  _;              # 匹配任何主机名

       location /dev-api/ {
            # 配置代理到后端服务的地址
            proxy_pass http://127.0.0.1:8080/;
            
            # 保持长连接
            proxy_http_version 1.1;
            proxy_set_header Connection "";

            # 将客户端的Host头转发到后端
            proxy_set_header Host $host;

            # 设置超时时间
            proxy_connect_timeout 600s;
            proxy_send_timeout 600s;
            proxy_read_timeout 600s;

            # 保持客户端的原始请求头信息
            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;
            
        }


        location / {
            # 配置前端资源的路径
            root D:\\OOP2\\project\\SDP\\smart-health-care\\ruoyi-ui\\dist;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        # 基本安全头信息
        add_header X-Content-Type-Options nosniff;
        add_header X-Frame-Options DENY;
    }

四、Nginx开启gzip压缩及请求频率限制

user nginx;
worker_processes auto;
worker_rlimit_nofile 3000;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    types_hash_max_size 4096;

    send_timeout 200s;
    keepalive_timeout 200s;
    keepalive_requests 5000;
    # 不显示版本号
    server_tokens off;


    # 开启动态压缩
    gzip  on;  # 开启 gzip
    gzip_min_length 2k;# 超过 2kb 进行压缩
    gzip_disable msie6; # ie6 不适用 gzip
    gzip_types text/css application/javascript text/javascript image/jpeg image/png image/gif; # 需要处理的文件

    # 开启静态加载本地的gz文件   
    # gzip_static on;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    client_max_body_size 1000M; #(设置客户端请求体最大值) 
    client_body_buffer_size 1000M; #(配置请求体缓存区大小) 
    fastcgi_intercept_errors on;

    # 定义一个请求频率限制的区域
    # $binary_remote_addr 表示远程地址的二进制形式,节省内存
    # zone=mylimit:100m 指定了共享内存区域的名字和大小,这里设置为 100 MB
    # rate=5r/s 设置了每个 IP 地址每秒处理的请求上限为 10 个
    limit_req_zone $binary_remote_addr zone=mylimit:150m rate=28r/s;

    # 定义一个错误页面,当请求被限制时返回 403 Forbidden
    limit_req_status 403;

    server {
        
        location /dev-api/ {
            # 在这个 location 中应用请求频率限制
            # zone=mylimit 指定了使用哪个共享内存区域
            # burst=10 设置了超出频率限制时允许的请求数量为 10 个
            # nodelay 表示立即处理这些超出速率的请求,延迟处理
            limit_req zone=mylimit burst=15;

            # 添加对所有请求的处理
            add_header 'Access-Control-Allow-Origin' 'https://www.xxxx.cn';  # 允许跨域请求
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';  # 允许的请求方法
            add_header 'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept';  # 允许的请求头
            add_header 'Access-Control-Allow-Credentials' 'true';  # 允许携带凭证

            # 处理OPTIONS预检请求
            if ($request_method = 'OPTIONS') {
                return 204;  # 返回成功状态码
            }

            # 这里配置代理到后端服务的地址
            proxy_pass http://127.0.0.1:19008/;
        }

        location / {
            # 这里配置前端资源的路径
            root /usr/local/project/mac-fxb/dist;
            index index.html index.htm;
            try_files $uri $uri/ /index.html;
        }

        location ~ \.(jpg|jpeg|png|gif|svg)$ {
            proxy_pass http://xxxx.cn:8848;
            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 http;
        }

         # 定义一个用于返回 403 Forbidden 错误的 location
        error_page 403 /403.html;
        location = /403.html {
            root /etc/nginx/;
            internal; # 表示这个 location 只能内部重定向,不能直接访问
        }
    }
}

五、nginx 子路径配置

location /health-care {
         # 配置前端资源的路径,将 /health-care 请求路径映射到实际的项目打包输出目录
         alias /usr/local/project/item_ruoyi/dist;

         # 指定默认访问的文件,index.html 或 index.htm
         index index.html index.htm;

         # 尝试访问 URI(即请求路径),如果文件或目录不存在,则将请求重定向到 /health-care/index.html
         # 这是为了支持 Vue Router 等前端路由方式(history 模式)
         try_files $uri $uri/ /health-care/index.html;
     }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七@归七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值