侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

详解Ngigx+Tomcat配置动静分离,负载均衡

技术  /  管理员 发布于 7年前   190

由于公司使用过Ngnix,对于刚接触Nginx来说,感觉有些好奇,于是研究了下。

本人在windows下使用的版本是nginx-1.8.1:

1. 启动Ngnix

双击nginx-1.8.1文件夹中nginx.exe,当任务管理器中存在两个nginx进程时,则说明启动成功!

2. Ngnix常用命令

  • nginx -s stop 强制关闭
  • nginx -s quit 安全关闭
  • nginx -s reload 改变配置文件的时候,重启nginx工作进程,来时配置文件生效 
  •  nginx -s reopen 打开日志文件

3. Nginx配置

下面配置综合了网上的资料,记下,防止自己忘记。

#Nginx所用用户和组#user nobody;#工作的子进程数量(通常等于CPU数量或者2倍于CPU)worker_processes 1;#错误日志存放路径#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#指定pid存放文件#pid    logs/nginx.pid;events {  #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue  #use epoll;  #使用epoll模型提高性能 win下不需要  #use epoll;  #允许最大连接数  worker_connections 1024;}http {  #扩展名与文件类型映射表  include    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 logs/access.log main;  # 启用内核复制模式,应该保持开启达到最快IO效率  sendfile    on;  #tcp_nopush   on;  #keepalive_timeout 0;  # HTTP1.1支持持久连接alive  # 降低每个连接的alive时间可在一定程度上提高可响应连接数量,所以一般可适当降低此值  keepalive_timeout 65;  # 启动gzip压缩功能设置,有效降低网络流量  gzip on;  gzip_min_length 1k;  #最小1K  gzip_buffers  4 16k;  gzip_http_version 1.0;  gzip_comp_level 2;  gzip_types text/plain application/x-javascripttext/css application/xml;  gzip_vary on;    # 静态文件缓存  # 最大缓存数量,文件未使用存活期  open_file_cache max=655350 inactive=20s;  # 验证缓存有效期时间间隔  open_file_cache_valid 30s;  # 有效期内文件最少使用次数  open_file_cache_min_uses 2;    #xbq add  #upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.  #weight为轮询权重,根据不同的权重分配可以用来平衡服务器的访问率。  upstream hostname {    server 127.0.0.1:9000 max_fails=0 weight=2;    server 127.0.0.1:9001 max_fails=0 weight=2;  }  server {    listen    8181;    server_name localhost;    #charset koi8-r;    #access_log logs/host.access.log main;    root /img; #在nginx-1.8.1文件夹中新建img文件夹,用于存放静态资源        location / {      #root  html;      #index index.html index.htm;      #xbq add      proxy_pass http://hostname;      #下面三条指令允许重新定义和添加一些将被转移到被代理服务器的请求头部信息      # 请求头中Host信息      proxy_set_header Host $host;      # 真实的客户端IP      proxy_set_header X-Real-IP $remote_addr;      # 代理路由信息,此处取IP有安全隐患      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;      # 真实的用户访问协议      proxy_set_header X-Forwarded-Proto $scheme;# 默认值default,      # 后端response 302时 tomcat header中location的host是http://192.168.1.62:8080      # 因为tomcat收到的请求是nginx发过去的, nginx发起的请求url host是http://192.168.1.62:8080      # 设置为default后,nginx自动把响应头中location host部分替换成当前用户请求的host部分      # 网上很多教程将此值设置成 off,禁用了替换,      # 这样用户浏览器收到302后跳到http://192.168.1.62:8080,直接将后端服务器暴露给浏览器      # 所以除非特殊需要,不要设置这种画蛇添足的配置      proxy_redirect default;      client_max_body_size 10m;  #允许客户端请求的最大单文件字节数      client_body_buffer_size 128k; #缓冲区代理缓冲用户端请求的最大字节数      proxy_connect_timeout 90;  #nginx跟后端服务器连接超时时间      proxy_read_timeout 90;   #连接成功后,后端服务器响应时间      proxy_buffer_size 4k;    #设置代理服务器(nginx)保存用户头信息的缓冲区大小      proxy_buffers 6 32k;    #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置      proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)      proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传          }        #xbq add    #配置Nginx动静分离,定义的静态页面直接从/usr/nginxStaticFile(Nginx发布目录)读取。    location ~\.(gif|jpg|jpeg|png|css|js|php)$ {#expires定义用户浏览器缓存的时间为7天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力  E:/staticResource;      expires 7d;    }        #xbq add    #启用nginx status 监听页面    location /nginxstatus {      stub_status on;      access_log on;    }    #error_page 404       /404.html;    # redirect server error pages to the static page /50x.html    #    error_page  500 502 503 504 /50x.html;    location = /50x.html {      root  html;    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80    #    #location ~ \.php$ {    #  proxy_pass  http://127.0.0.1;    #}    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000    #    #location ~ \.php$ {    #  root      html;    #  fastcgi_pass  127.0.0.1:9000;    #  fastcgi_index index.php;    #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;    #  include    fastcgi_params;    #}    # deny access to .htaccess files, if Apache's document root    # concurs with nginx's one    #    #location ~ /\.ht {    #  deny all;    #}  }  # another virtual host using mix of IP-, name-, and port-based configuration  #  #server {  #  listen    8000;  #  listen    somename:8080;  #  server_name somename alias another.alias;  #  location / {  #    root  html;  #    index index.html index.htm;  #  }  #}  # HTTPS server  #  #server {  #  listen    443 ssl;  #  server_name localhost;  #  ssl_certificate   cert.pem;  #  ssl_certificate_key cert.key;  #  ssl_session_cache  shared:SSL:1m;  #  ssl_session_timeout 5m;  #  ssl_ciphers HIGH:!aNULL:!MD5;  #  ssl_prefer_server_ciphers on;  #  location / {  #    root  html;  #    index index.html index.htm;  #  }  #}}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    HTTP 错误 403.1禁止访问:禁止执行访问的完美解决方法
    下一条:
    virtualbox安装增强功能时【未能加载虚拟光盘】的问题解决
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客