hjyssg / ShiguReader

硬核宅宅资源管理器. Ultimate Manga Resource Manager
MIT License
400 stars 45 forks source link

用nginx进行load balancing #217

Open hjyssg opened 1 year ago

hjyssg commented 1 year ago

实验性尝试了一下nginx的负载均衡,可以的。挺有意思的,但单进程性能已经够了。


worker_processes auto;

events {
    worker_connections 1024;
}

http {
    # 本地起两个express server
    upstream mybalance01 {
        # weight越高收到的请求越多
        server 127.0.0.1:3000 weight=1 max_fails=5 fail_timeout=3;
        server 127.0.0.1:34213 weight=2 max_fails=5 fail_timeout=3;
    }

    # 缓存目录
    # NOTE:可以用nginx的缓存替换掉代码内部GET缓存
    # proxy_cache_path tmp/nxcache levels=1:2 keys_zone=my_cache:10m inactive=30s;
    # 定义 MIME 类型和默认字符集
    # include /etc/nginx/mime.types;
    # default_type application/octet-stream;
    # include       mime.types;
    # default_type  application/octet-stream;
    # sendfile        on;
    # keepalive_timeout  65;

    # 反向代理配置
    server {
        listen 80;

        # 下载功能进行负载均衡
        location /api/download/ {
            proxy_pass http://mybalance01;
        }

        # 其他请求都主服务器
        # NOTE:哪天数据库不用sqlite,换成mysql之类的,连这个也可以换成负载均衡
        location / {
            proxy_pass http://localhost:3000;
        }
    }
}
hjyssg commented 1 year ago

参考

https://www.cnblogs.com/taiyonghai/p/9402734.html https://nginx.org/en/docs/beginners_guide.html#control