博客
关于我
Nginx动静分离实现负载均衡
阅读量:674 次
发布时间:2019-03-16

本文共 1442 字,大约阅读时间需要 4 分钟。

在使用Debian环境部署web项目时,Nginx和Tomcat是常用且高效的组合。以下是基于Nginx配置的实用指南。

Nginx配置说明

默认安装Nginx后,/etc/nginx/nginx.conf 是主要配置文件。以下是关键配置项:

  • 进程参数配置
  • worker_processes 8;
    worker_rlimit_nofile 65535;

    根据CPU核心数设置工作进程数,并与ulimit一致调整文件描述数。

    1. 网络模型优化
    2. events {
      use epoll;
      worker_connections 65535;
      }

      epoll模型提升性能, worker_connections指定单个进程的最大连接数。

      1. 缓存与压缩
      2. gzip on;
        gzip_min_length 1k;
        gzip_buffers 16 64k;

        开启gzip压缩,优化带宽使用。

        1. 负载均衡设置
        2. server {
          listen 80;
          server_name your domaine.com;
          location / {
          index index.html;
          proxy_pass http://backend_server;
          }
          location ~ /.*\.js|\.css|\.ico|\.png|.jpg|.woff {
          proxy_cache cache_zone;
          proxy_cache_valid 200 304 5d;
          proxy_pass http://backend_server;
          expires 30d;
          }
          location ~ /.* {
          proxy_pass http://backend_server;
          }
          }

          配置反向代理,合理分配静态和动态资源,确保负载均衡和缓存有效性。

          动静资源分离

          将Nginx配置为缓存性能优化。修改nginx.conf

          location ~ .\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
          proxy_cache cache_zone;
          proxy_cache_valid 200 304 5d;
          proxy_pass http://backend_server;
          expires 30d;
          }

          这段代码实现静态资源缓存,提升性能。

          动态页面反向代理

          确保动态页面访问Tomcat服务:

          location / {
          proxy_pass http://backend_server:8080;
          }

          SSL配置(可选)

          使用 自签名证书:

          server {
          listen 443 ssl;
          ssl on;
          ssl_certificate /path/client.pem;
          ssl_key_file /path/client.key.unsecure;
          }

          注意事项

        3. 权限设置

          user www www; 为Nginx设置权限。

        4. 防火墙检查

          关闭防火墙,确保Nginx服务正常。

        5. 缓存管理

          这些配置均衡分布至多台服务器,集群部署。

        6. 升级建议

          升级Nginx版本,获取最新功能和性能提升。

        7. 这配置将实现高效、稳定的资源分离和反向代理。

    转载地址:http://aiiqz.baihongyu.com/

    你可能感兴趣的文章
    npm install digital envelope routines::unsupported解决方法
    查看>>
    npm install 卡着不动的解决方法
    查看>>
    npm install 报错 EEXIST File exists 的解决方法
    查看>>
    npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
    查看>>
    npm install 报错 Failed to connect to github.com port 443 的解决方法
    查看>>
    npm install 报错 fatal: unable to connect to github.com 的解决方法
    查看>>
    npm install 报错 no such file or directory 的解决方法
    查看>>
    npm install 权限问题
    查看>>
    npm install报错,证书验证失败unable to get local issuer certificate
    查看>>
    npm install无法生成node_modules的解决方法
    查看>>
    npm install的--save和--save-dev使用说明
    查看>>
    npm node pm2相关问题
    查看>>
    npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
    查看>>
    npm run build报Cannot find module错误的解决方法
    查看>>
    npm run build部署到云服务器中的Nginx(图文配置)
    查看>>
    npm run dev 和npm dev、npm run start和npm start、npm run serve和npm serve等的区别
    查看>>
    npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
    查看>>
    npm scripts 使用指南
    查看>>
    npm should be run outside of the node repl, in your normal shell
    查看>>
    npm start运行了什么
    查看>>