@shjanken
2015-04-09T02:27:14.000000Z
字数 2418
阅读 1111
学习笔记 linux
===
error log,workprosses 等等poll,epoll 等等)http 相关的配置
server {.... ## 其他配置项location /status {stub_status on;access_log off;allow 192.168.0.1; ## 允许访问的IPallow 192.168.0.0/24;allow 127.0.0.1;deny all;}}
状态页面各项数据的意义:
active connections: 当前 Nginx 正在处理的活动连接数
serveraccepts handled request: 总共处理的链接, 成功处理的握手次数, 总共处理了多少请求
reading: Nginx 读取到客户端的 Header 信息数
writing: Nginx 返回给客户端的 Header 信息数
waiting: 开启 keep-alive 的情况下, 这个值 等于 active - (reading + writing), 就是 Nginx 已经处理完成正在等候下一次请求指令的驻留链接.
使用反向代理的指令是 proxy_pass.
server {listen 80;sever_name www.sucem.comlocation /{后端服务器}location /forum/ {/* 将 http://www.sucem.com/forum 的访问代理到http://www.sucem.com:8080/bbs 应用上 */proxy_pass: http://www.sucem.com:8080/bbs/}}
**注意: 如果在 Location 中专用了模式匹配(~, ~* , ^~, = 等符号),这不能在方向代理中写 URI。 比如 不能写 http://www.sucem.com:8080/bbs/ 只能到 http://www.sucen.com:8080 为止。Nginx会自动根据匹配到的URI附加在网址最后 **
在反向代理中添加真实的客户端地址
需要用到的模块参数:
/* 在配置文件中配置 location,在请求中添加首部 */server {....location / {proxy_pass: http://192.168.100.101:8080;proxy_set_header X-Real-IP $remote_addr;}}/* 配置完成之后在还需要在访问日志中记录该值(各个服务器记录的方法都不一样) */
Nginx 的反向代理功能主要是由 HttpUpstreamModule 模块提供的。
该模块通过 upstream 配置块提供简单的负载均衡功能(轮询,最少链接,客户端IP)。--官方文档
如:
upstream backend {server backend1.example.com weight=5;server backend2.example.com:8080;server unix:/tmp/backend3;}server {location / {proxy_pass http://backend;}}
参数都比较简单。不做解释了,需要注意的是,在 server 配置项中,一定不能加上http !
Nginx 对后端服务器进行健康状况检查
max_fails , fail_timeout如果所有的后端服务器都 down 了,这可以使用 sorry server
server {listen 8080;servername localhost;root /web/errorpages;index index.html;}upstream backend {server backend1.example.com weight=5;server backend2.example.com:8080;server localhost:8080 down // down 表示这是一个 sorry server}
Nginx 的 ip_hash
通过 ip_hash 将同一个客户端始终定义到同一台服务器上, ip_hash 在 upstream 块中定义。但是使用了 ip_hash 以后就不能使用 sorry server 了
Nginx 的缓存需要提供内存空间(存放键和对象的元数据)以及磁盘空间(用来存放真实数据)
使用指令 proxy_cache_path 来创建缓存。
可以使用的参数:
example
proxy_cache_path /nginx/cache/first levels=2:1 keys_zone=first:20m max_size=1g;proxy_cache_valid 200 10m //缓存200响应结果 10分钟server {.../* 添加一个响应首部,响应服务器地址 */add_header X-via $server_addr;/* 添加首部记录缓存状态 */add_header x_Cache $upstream_cache_statuslocation / {...proxy_cache first;}}