1、安装监控模块

  Nginx中的stub_status模块主要用于查看Nginx的一些状态信息.

  本模块默认是不会编译进Nginx的,如果你要使用该模块,则要在编译安装Nginx时指定:

.  /configure –with-http_stub_status_module

  配置文件中启动监控

  server下:

  location /nginx-status {

allow --------

allow --------//允许的ip

deny all;//

stub_status on;

access_log  off;

}

2、启用websocket代理

  由于websocket只有在连接是使用了http协议,之后边返回101改变协议为websocket,所以需要特殊对websocket做配置

  http://nginx.org/en/docs/http/websocket.html

location /chat/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

A more sophisticated(复杂) example in which a value of the “Connection” header field in a request to the proxied server depends on the presence of the “Upgrade” field in the client request header:

http {
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
} server {
... location /chat/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}
}

By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds. This timeout can be increased with the proxy_read_timeout directive. Alternatively, the proxied server can be configured to periodically send WebSocket ping frames to reset the timeout and check if the connection is still alive.

  

3、注意:如果代理的地址中,有下划线存在,会被nginx忽略,于是带下划线的请求都会404(也不一定)

syntax: underscores_in_headers on | off;
default:    underscores_in_headers off;
context:    http, server

Enables or disables the use of underscores in client request header fields. When disabled, request header fields whose names contain underscores are marked as invalid and are subject to the ignore_invalid_headers directive.

syntax: ignore_invalid_headers on | off;
default:    ignore_invalid_headers on;
context:    http, server
Controls whether header fields with invalid names should be ignored. Valid names are composed of English letters, digits, hyphens, and possibly underscores (as controlled by the  underscores_in_headers directive).

If you do not explicitly set underscores_in_headers on, NGINX will silently drop HTTP headers with underscores (which are perfectly valid according to the HTTP standard). This is done in order to prevent ambiguities when mapping headers to CGI variables as both dashes and underscores are mapped to underscores during that process.

4、nginx的proxy_redirect配置项的作用

  http://bluedest.iteye.com/blog/740302

5、多服务器配置

  http://www.2cto.com/os/201411/355366.html

我的配置文件:详解

##定义Nginx运行的用户和用户组,user user [group],没有group是默认和user相同
user nginx www; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]
#最后一个参数为级别
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #进程文件,进程pid放到这个文件中,用于作为参数使用
pid logs/nginx.pid; #nginx进程数,建议设置为等于CPU总核心数。
#设置为“auto”将尝试自动检测它
worker_processes 2; #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulimit -n)与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致。
#如果不确定,这个不要配,nginx会自动使用合适的值,也可以叫做文件描述符数量
#worker_rlimit_nofile 65535; #event指令,工作模式与连接数上限
events {
#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
use epoll;
#告诉nginx收到一个新连接通知后接受尽可能多的连接。
multi_accept on;
#单个进程最大连接数(最大连接数=连接数*进程数)
worker_connections 2048;
} http { #设置nginx是否将存储访问日志。关闭这个选项可以让读取磁盘IO操作更快(aka,YOLO)。
#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; #文件扩展名与文件类型映射表
include mime.types; #默认文件类型
default_type application/octet-stream; #默认编码
#charset utf-8; #服务器名字的hash表大小,用于使用负载均衡时防止串session
server_names_hash_bucket_size 128; #server_tokens 并不会让nginx执行的速度更快,但它可以关闭在错误页面中的nginx版本数字,这样对于安全性是有好处的。
server_tokens off; #可以让sendfile()发挥作用。sendfile()可以在磁盘和TCP socket之间互相拷贝数据(或任意两个文件描述符)。
#Pre-sendfile是传送数据之前在用户空间申请数据缓冲区。之后用read()将数据从文件拷贝到这个缓冲区,
#write()将缓冲区数据写入网络。sendfile()是立即将数据从磁盘读到OS缓存。
#因为这种拷贝是在内核完成的,sendfile()要比组合read()和write()以及打开关闭丢弃缓冲更加有效
##开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
sendfile on; #告诉nginx在一个数据包里发送所有头文件,而不一个接一个的发送
#tcp_nopush on;
#告诉nginx不要缓存数据,而是一段一段的发送–当需要及时发送数据时,就应该给应用设置这个属性,这样发送一小块数据信息时就不能立即得到返回值。
#tcp_nodelay on; #给客户端分配keep-alive链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让ngnix持续工作的时间更长。秒。
keepalive_timeout 65; #设置请求头和请求体(各自)的超时时间。我们也可以把这个设置低些。
client_header_timeout 10;
client_body_timeout 10; #开启目录列表访问,合适下载服务器,默认关闭。
autoindex on; #告诉nginx关闭不响应的客户端连接。这将会释放那个客户端所占有的内存空间。
reset_timedout_connection on; #send_timeout 指定客户端的响应超时时间。这个设置不会用于整个转发器,而是在两次客户端读取操作之间。如果在这段时间内,客户端没有读取任何数据,nginx就会关闭连接。
send_timeout 10; #limit_conn_zone设置用于保存各种key(比如当前连接数)的共享内存的参数。5m就是5兆字节,这个值应该被设置的足够大以存储(32K*5)32byte状态或者(16K*5)64byte状态。
limit_conn_zone $binary_remote_addr zone=addr:5m; #limit_conn为给定的key设置最大连接数。这里key是addr,我们设置的值是100,也就是说我们允许每一个IP地址最多同时打开有100个连接。
limit_conn_zone $binary_remote_addr zone=addr:5m; # gzip压缩功能设置
gzip on;
#为指定的客户端禁用gzip功能。我们设置成IE6或者更低版本以使我们的方案能够广泛兼容。
gzip_disable "msie6";
#告诉nginx在压缩资源之前,先查找是否有预先gzip处理过的资源。这要求你预先压缩你的文件(在这个例子中被注释掉了),从而允许你使用最高压缩比,这样nginx就不用再压缩这些文件了(想要更详尽的gzip_static的信息,请点击这里)。
#gzip_static on;
#允许或者禁止压缩基于请求和响应的响应流。我们设置为any,意味着将会压缩所有的请求。
#gzip_proxied
#设置对数据启用压缩的最少字节数。如果一个请求小于1000字节,我们最好不要压缩它,因为压缩这些小的数据会降低处理此请求的所有进程的速度。
gzip_min_length 1k;
#压缩缓冲区
gzip_buffers 4 16k;
##压缩版本(默认1.1,前端如果是squid2.5请使用1.0)
gzip_http_version 1.0;
#设置数据的压缩等级。这个等级可以是1-9之间的任意数值,9是最慢但是压缩比最大的。我们设置为4,这是一个比较折中的设置。
gzip_comp_level 6;
#设置需要压缩的数据格式。上面例子中已经有一些了,你也可以再添加更多的格式。mime用空格隔开。
gzip_types text/html text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on; #上传文件大小限制
#client_header_buffer_size 32k; #设定请求缓存
large_client_header_buffers 4 64k; # http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/nginx/proxy_temp 1 2; # 设定负载均衡后台服务器列表
#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。
#backend为服务器name
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
#server 192.168.80.121:80 weight=3;
#server 192.168.80.122:80 weight=2;
#server 192.168.80.123:80 weight=3;
} # FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度。下面参数看字面意思都能理解。
#fastcgi_connect_timeout 300;
#fastcgi_send_timeout 300;
#fastcgi_read_timeout 300;
#fastcgi_buffer_size 64k;
#fastcgi_buffers 4 64k;
#fastcgi_busy_buffers_size 128k;
#fastcgi_temp_file_write_size 128k; # cache informations about file descriptors, frequently accessed files
# can boost performance, but you need to test those values
#打开缓存的同时也指定了缓存最大数目,以及缓存的时间。我们可以设置一个相对高的最大时间,这样我们可以在它们不活动超过20秒后清除掉。
open_file_cache max=100000 inactive=20s;
#在open_file_cache中指定检测正确信息的间隔时间。
open_file_cache_valid 30s;
#定义了open_file_cache中指令参数不活动时间期间里最小的文件数。
open_file_cache_min_uses 2;
#指定了当搜索一个文件时是否缓存错误信息,也包括再次给配置中添加文件。我们也包括了服务器模块,这些是在不同文件中定义的。如果你的服务器模块不在这些位置,你就得修改这一行来指定正确的位置。
open_file_cache_errors on;
##
# Virtual Host Configs
# aka our settings for specific servers
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*; # 很重要的虚拟主机配置
server {
listen 80;
#域名可以有多个,用空格隔开
server_name localhost itoatest.example.com;
#不确定在这里配有没有用
#index index.html index.htm index.php;
#根目录,不确定这里是否有效,一般在location下,这里有可能是全局配置
#在这个目录下根据请求地址找文件.
#root /home/html/angular; #日志格式设定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定义本虚拟主机的访问日志
access_log logs/host.access.log main; charset utf-8; #对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm; proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } #静态文件,nginx自己处理,不去backend请求tomcat,正则表达式以~开头
location ~* /download/ {
root /apps/oa/fs;
} #php文件,交给CGI来处理
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
} #本地动静分离反向代理配置
#所有jsp的页面均交由tomcat或resin处理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
} location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
expires 15d;
} #图片缓存时间设置
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
#缓存时长
expires 7d;
} #JS和CSS缓存时间设置
location ~ .*\.(js|css)?$
{
expires 1h;
} location /nginx_status {
stub_status on;
access_log off;
//允许哪个ip查看
allow 192.168.10.0/24;
deny all;
} #设定查看Nginx状态的地址
#location /NginxStatus {
# stub_status on;
# access_log on;
# auth_basic "NginxStatus";
# auth_basic_user_file conf/htpasswd;
# #htpasswd文件的内容可以用apache提供的htpasswd工具来产生。
#} #禁止直接访问WEB-INF文件夹
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
} #配置二级域名,$host代表访问的地址,用正则表达式匹配,再使用rewrite模块把地址重写
if ( $host ~* (\b(?!www\b)\w+)\.\w+\.\w+ ) {
set $subdomain $1;
}
location / {
rewrite ^/$ /blog/$subdomain last;
proxy_pass http://www.mydomain.com/;
} } ## 其它虚拟主机,server 指令开始
server {
listen 80;
server_name node.ailiailia.com; location / {
proxy_pass http://localhost:3000/;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 60;
proxy_read_timeout 600;
proxy_send_timeout 600;
} location /status {
stub_status on;
access_log off;
} location /.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 30d;
} location /.(js|css)?$ {
expires 12h;
}
}
}

nginx中级应用的更多相关文章

  1. nginx中级应用-续

    1.server下配置的每个location,都需要有自己的一套代理配置 即要么加入: root  某个目录 要么加入 proxy_pass 某个地址;  proxy_redirect off; # ...

  2. nginx篇中级用法之反向代理(七层调度)

    环境: 两台后端web,一台代理服务器 web1:eth0:192.168.2.100/24   httpd做一个web web2:eth0:192.168.2.200/24   httpd做一个we ...

  3. Linux中级之负载均衡(lvs,nginx,haproxy)、中间件

    一.负载均衡的概念 1.系统的扩展方式: scale up:向上扩展 scale out:向外扩展 2.集群类型:  LB(Load Balancing).HA(high availability) ...

  4. nginx ssl证书安装配置

    原理图: - 客户端生成一个随机数 random-client,传到服务器端(Say Hello) - 服务器端生成一个随机数 random-server,和着公钥,一起回馈给客户端(I got it ...

  5. nginx - ssl 配置 - globelsign ssl

    前提: 3个文件 - domain.csr.domain.key.xxx.cer 简述: 1. 本地生成 .key文件  [附件] 2. 再利用key文件,生成csr(certificate Secu ...

  6. 对 Linux 初级、中级、高级用户非常有用的 60 个命令

    对 Linux 初级.中级.高级用户非常有用的 60 个命令 初级篇: 你打算从Windows换到Linux上来,还是你刚好换到Linux上来?哎哟!!!我说什么呢,是什么原因你就出现在我的世界里了. ...

  7. Nginx对于图片,js等静态文件的缓存设置

    以下是自学it网--中级班上课笔记 网址:www.zixue.it Nginx对于图片,js等静态文件的缓存设置 注:这个缓存是指针对浏览器所做的缓存,不是指服务器端的数据缓存. 主要知识点: loc ...

  8. 在NGINX上配置HTTPS---血的教训--要重启NGINX

    重启,不是重载!!! 是STOP & START 而不是RELOAD!!! 纠结了好几天...(难道有的NGINX上不用重启????) 你妹的,上次也是,,PHP-FPM,将一个PHP的程序连 ...

  9. [Open Source] 负载均衡之Nginx

    目录 简介 常用命令 功能 代理 反向代理 集群 HTTP集群 TCP集群 重定向 静态文件 HTTPS配置 常见问题 简介 Nginx ("engine x") 是一款轻量级,高 ...

随机推荐

  1. Flask之视图(一)

    2.关于Flask 知识点 从Hello World开始 给路由传递参数 返回状态码 重定向 正则URL 设置cookie和获取cookie 扩展 上下文 请求钩子 Flask装饰器路由的实现 Fla ...

  2. linux anaconda 管理 python 包

    1.下载 anaconda https://www.continuum.io/downloads 2.安装anaconda 3.conda install package-name //利用anaco ...

  3. 基于七牛Python SDK写的一个批量下载脚本

    前言 上一篇基于七牛Python SDK写的一个同步脚本所写的脚本只支持上传,不支持文件下载. 虽然这个需求不太强烈,但有可能有人(在备份.迁移时)需要,而官方有没提供对应的工具,所以我就把这个功能也 ...

  4. mybatis No enum const class org.apache.ibatis.type.JdbcType.Date 坑爹的配置

    转自:https://lihaiming.iteye.com/blog/2248059 在ibatis中不需要关注这些参数 而转到mybatis后 如果字段值为空 必须设置jdbcType如inser ...

  5. rails 网站字体

    方法1,在rubymine下查找所有css,scss,sass,less,修改所有带font-family的内容,删除public文件夹下面的缓存css,查看效果.如 body { backgroun ...

  6. Spring cloud Eureka高可用 - Windows 7 hosts文件立即生效

    hosts 文件所在位置 c:/windows/system32/drivers/etc/hosts 左下角 搜索框 搜索 cmd 弹出命令框 输入 ipconfig /displaydns 显示所有 ...

  7. Linux系统构架 - HA集群配置

    一.安装 HA 即(high available)高可用,又被叫做双机热备,用于关键性业务. 简单理解就是,有两台机器 A 和 B,正常是 A 提供服务,B 待命闲置,当 A 宕机或服务宕掉,会切换至 ...

  8. MyBatis 学习记录7 一个Bug引发的思考

    主题 这次学习MyBatis的主题我想记录一个使用起来可能会遇到,但是没有经验的话很不好解决的BUG,在特定情况下很容易发生. 异常 java.lang.IllegalArgumentExceptio ...

  9. js使用浏览器的另存为下载文件

    页面上的页面如下: 我需要根据返回的url下载文件: js: //判断浏览器类型 function myBrowser(){ var userAgent = navigator.userAgent; ...

  10. zookeeper会话超时 链接超时的排查

    1.会话概述 在ZooKeeper中,客户端和服务端建立连接后,会话随之建立,生成一个全局唯一的会话ID(Session ID).服务器和客户端之间维持的是一个长连接,在SESSION_TIMEOUT ...