一:简介
Tomcat在高并发环境下处理动态请求时性能很低,而在处理静态页面更加脆弱。虽然Tomcat的最新版本支持epoll,但是通过Nginx来处理静态页面要比通过Tomcat处理在性能方面好很多。
二:下载安装(以windows环境为例)
1、下载地址
下载地址:点击这里
2、目录结构
<code> Nginx- |_ conf 配置目录 |_ contrib |_ docs 文档目录 |_ logs 日志目录 |_ temp 临时文件目录 |_ html 静态页面目录 |_ nginx.exe 主程序 </code>
三:启动和停止nginx服务
cmd进入nginx解压缩目录
执行start nginx,就可以启动服务(或者nginx或者nginx.exe)
建议使用第一种,
其他二种会使你的cmd窗口一直处于执行中,不能进行其他命令操作
执行nginx -s stop可以停止nginx服务
执行nginx -t 可以检查nginx配置文件是否正确。
四:nginx主配置文件nginx.conf
所有配置文件下载,请点击这里
<code><span>#Nginx所用用户和组</span><span>#user niumd niumd;</span><span>#工作的子进程数量(通常等于CPU数量或者2倍于CPU)</span> worker_processes <span>2</span>; <span>#错误日志存放路径</span><span>#error_log logs/error.log;</span><span>#error_log logs/error.log notice;</span> error_log logs/error.log info; <span>#指定pid存放文件</span> pid logs/nginx.pid; events { <span>#使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue</span><span>#use epoll;</span><span>#允许最大连接数</span> worker_connections <span>2048</span>; } http { <span>include</span> mime.types; default_type application/octet-stream; <span>#定义日志格式</span><span>#log_format main '$remote_addr - $remote_user [$time_local] $request '</span><span># '"$status" $body_bytes_sent "$http_referer" '</span><span># '"$http_user_agent" "$http_x_forwarded_for"';</span><span>#access_log off;</span> access_log logs/access.log; client_header_timeout <span>3</span>m; client_body_timeout <span>3</span>m; send_timeout <span>3</span>m; client_header_buffer_size <span>1</span>k; large_client_header_buffers <span>4</span><span>4</span>k; sendfile <span>on</span>; tcp_nopush <span>on</span>; tcp_nodelay <span>on</span>; <span>#keepalive_timeout 75 20;</span><span>include</span> gzip.conf; upstream localhost { server localhost:<span>8080</span> weight=<span>5</span>; server localhost:<span>9091</span> weight=<span>1</span>; } server { listen <span>80</span>; server_name localhost; location / { proxy_connect_timeout <span>3</span>; proxy_send_timeout <span>30</span>; proxy_read_timeout <span>30</span>; proxy_pass http://localhost; } } } </code>
五: 负载均衡权重配置
<code> upstream localhost { <span>server</span> localhost:<span>8080</span> weight=<span>5</span>; <span>server</span> localhost:<span>9091</span> weight=<span>1</span>; }</code>
以上就介绍了nginx负载均衡配置,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。