今天整理下 之前干活 nginx 配置常用模块;

nginx 配置主要是分为4个部分

1、main 全局设置

2、server 主机设置   -- 指定主机与端口

3、upstream   负载均衡服务器设置   --   反向代理设置;

4、location URL 匹配特定位置的设置   -- 匹配网页位置;

可以通过location 指令实现对nginx 对动静网页进行过滤处理;

大概也是这样 :

events {

...

}

http {

....

server {

localtion {

...

}

...

}

server {

include /nginx/conf/vhosts/www1.com.conf

## // 配置虚拟主机配置内容指向位置;其实也是一个 server 虚拟主机配置;

}

}

一、【 nginx  全局配置 】

user  nobody;

## // user 主模块  ,指定nginx worker 进程的运行用户组

worker_processes  1;

## // worker_processes  开启进程数 每个nginx进程平均消耗 10-12内存 (CPU 和内核进程数一样)

error_log  logs/error.log;

error_log  logs/error.log  notice;

error_log  logs/error.log  info;

error_log  日志输出级别 ;有 debug info notice warn error crit

pid        logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 65535;

## // worker_rlimit_nofile  nginx进程打开的最多文件描述符数目  65535 (ulimit -n 65535 设置)

events {

use epoll;

worker_connections  1024;

}

## // events 设置工作模式和设置连接数;

## // 默认是 use epoll;

## // 指定nginx的工作模式标准和高效模式:select Poll 标准 和 kqueue epoll rtsig /dev/poll高效)

worker_connections 65535;

## // 事件模块,定义内个进程的最大连接数;

## // 最大客户端的链接数一般由 worker_processes 和 worker_connections 决定的;max_clinet = worker_processes* worker_connections

## // 在作为反向代理时变为  max client =worker_processes*worker_connections/4

二、 【  nginx HTTP 服务器配置  】

http {

include       mime.types;

## // 主模块;实现对配置文件包含的文件设定,可以减少主配置文件的复杂度;

default_type  application/octet-stream;

## // http 的核心模块指令;默认是设置为二进制流;就是当文件类型未定义时使用这种解析;例如不配置PHP时打开PHP文件就是下载;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

#                  '$status $body_bytes_sent "$http_referer" '

#                  '"$http_user_agent" "$http_x_forwarded_for"';

## // 日志输入格式; main就是日志输入格式的名称; 在下面access_log 中引用;

#access_log  logs/access.log  main;

sendfile        on;

## // 开启高效文件传输模式;  开启下面两个可以有效防止网络阻塞;

#tcp_nodely     on;

#tcp_nopush     on;

charset  utf-8;

## // html 字符集格式 ;

server_names_hash_bucket_size 128;

## // 多虚拟主机服务器名字的hash表大小

client_max_body_size 8m;

## // 客户端请求的最大的单个文件字节数

client_header_buffer_size 32k;

## // 客户端请求  client_header_buffer 大小;1KB 缓冲区就是很大的;

large_client_header_buffers 4 32k;

## // 客户端请求中较大的消息头的缓存最大数量和大小。

#keepalive_timeout  0;

keepalive_timeout  65;

## // 客户端链接保持活动的超时时间 ;

client_body_timeout 10;

## // 客户端请求主题读取时间  默认是60;超时提示 request time out (408)

client_header_timeout 10;

## // 客户端请求头读取的超时时间; 如果没返回数据  提示也是 request time out (408)

send_timeout  10;

## // 设置响应客户端的超时时间;

【 ngxin proxy开启Cache缓存, 需要安装proxy 插件】

proxy_temp_path  /tmp/proxy_temp_dir;

## //  设置缓存位置

proxy_cache_path /tmp/proxy_cache_dir   levels = 1:2 keys_zone = cache_one:200m inactive =1d    max_size =30g;

## //  设置缓存区名称  内存缓存空间大小  1天内容没有访问自动清除 缓存设置大小 ;

三、 【 负载均衡配置  】

nginx 负载包括四种

1、轮询 按照请求时间逐一分配到不同的后端服务器;

2、ip_hash  当调度为ip_hash时;后端服务器调度状态不能是 weight 和 backup

3、fair 根绝页面大小 和加载时间进行只能的负载;也就是根据响应时间分配;nginx本身不支持fair,需要下载nginx的 upstream_fair模块;

4、url_hash  按访问URL的hash结果来分配;每一个URL定向到同一个后端服务器;

upstream 是nginx 的和http upstream模块的,通过一个简单的调度计算来实现客户端IP到后端服务器的负载均衡;

upstream www1.com {

## //   定义负载均衡名称 www1.com

ip_hash;

## // 每个请求按访问IP的哈希结果分配 同一个IP访问固定访问一个后端服务器,有效解决了动态网页存在的session(会话)共享问题;

server 192.168.0.11:80 ;

server 192.168.0.12:80 down;

## //  down 表示当前 server暂时不参与负载均衡;backup 参数表示预留备份机器;

server 192.168.0.13:80 max_fails=3 fail_timeout=20s;

## // max_fails 表示请求失败次数 fails_timeout 失败孩子后暂停服务的时间;

}

四、 【 反向代理 配置方式  】

一中直接直接是配置在 HTTP 下;

一种是 配置 localtion /{  proxy_pass 调用 }

http {

upstream myserver {

## //   定义负载均衡组 myserver

server 192.168.0.11:80 weight=3 max_fails=3 fail_timeout=20s;

server 192.168.0.12:80 weight=3 max_fails=3 fail_timeout=20s;

server 192.168.0.13:80 weight=3 max_fails=3 fail_timeout=20s;

## // max_fails 表示请求失败次数 fails_timeout 失败孩子后暂停服务的时间;

}

server {

## //

listen 80;

server_name www.www1.com 192.168.0.10;

index index.html index.html;

root /web/html;

location / {

proxy_pass http://myserver

proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;

include /nginx/conf/proxy.conf;

## // nginx 反向代理通过 proxy.conf 模块实现的;通过include 指令包括一个文件;

}

}

}

/nginx/conf/proxy.conf  反向代理配置 如下:

proxy_redirect off;

## // 重定向 指定主机名 ;

proxy_set_header Host $host;

## // set_header 设置后端服务器获取主机名或者真实IP;以及代理者的真实IP地址;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_body_buffer_size 128k;

## // 客户端请求主题的缓冲区大小 保存本地再传输到客户端;

proxy_connect_timeout 90;

## // 与后端服务器链接超时时间  即握手响应的超时时间;

proxy_read_timeout 90;

## // 设置nginx从代理的后端服务器获取信息的时间; 表示连接建立成功 nginx响应等待后端的时间;

proxy_send_timeout 90;

## // 后端服务器数据回传时间

proxy_buffer_size   4k;

## // 缓冲区大小

proxy_buffer     4 32k;

## // 缓冲区数量和大小 ;nginx 从后端服务器获取的响应信息;

proxy_busy_buffer_size   64k;

## // 系统很忙时 缓冲区的大小  一般是缓冲区设置的两倍;

proxy_temp_file_write_size  64k;

## // 指定proxy临时文件的大小 ;

五、【  URL 匹配 】  -- location 实现对网页URL进行分析处理;

location ~ .*\.(gif|jpg|jpeg|png|bmg|swf)$ {

## // 扩展名为gif|jpg|jpeg|png|bmg|swf的静态文件都交给nginx处理;

root /nginx/web/www1;

expires  30d;

## // expires 指定静态文件的过期时间 30天;

}

location ~ ^ /(upload|html|down)/ {

## // 设置将upload和HTML 目录下的所有文件都交给nginx处理了;upload 和html 目录都必须在 www1 下面;

root /nginx/web/www1;

expires  30d;

}

location ~ .*.jsp$ {

index index.jsp;

proxy_pass http:## //localhost:8088;

## // 对此虚拟机下的动态网页进行过滤处理,将所有的jsp为后缀的文件都交给本机的8088端口处理;

}

六、【  防盗链配置 】  -- ## // 防盗链一般都是流媒体配置

location ~* \.(jpg|jpeg|png|bmg|swf|mp4|mp4|mmf|zip|rar|swf|flv)$ {

## // 对jpg|jpeg|png|bmg|swf|mp4|mp4|mmf|zip|rar|swf|flv 实行防盗链处理;

valid_referers none blocked *.www1.com www1.com;

## // www1.com 这个请求可以正常访问上面指定文件资源;

if ($invalid_referer) {

rewrite ^/ http:## //www.www1.com/img/error.gif;

#return 403;

}

}

location /p_w_picpaths {

root /nginx/web/html;

valid_referers none blocked *.www1.com www1.com

if ($invalid_referer) {

return 403;

}

}

七、 【 HTTP 开启gzip  】

gzip   on;                 ## //  开启 nginx在线实时压缩数据流;

gzip_min_length 1k;         ## //  允许压缩的页面最小字节

gzip_buffers   4 32k; ## //  表示 申请4个单位为16k 的内存作为压缩结果流缓存;

gzip_http_version   1.1 ; ## //  http 的版本 默认是1.1

gzip_comp_level   2;         ## //  gzip 压缩比 ;1--9 压缩比越小 处理越慢 ;9 压缩比越大 处理越小 CPU 消耗越多;

gzip_types    text/plain  application/x-javascript  text/css application/xml  ;

## // 压缩类型

gzip_vary  on; ## // 设置让前端的缓存服务器经过gzip压缩的页面; 例如 让 squid 缓存经过nginx压缩数据;

八、 【  虚拟主机配置 】

server { ## // 标识虚拟主机开始

listen       80; ## // 指定虚拟主机服务器端口

server_name  localhost; ## // 指定 IP地址或者域名; 多个域名用空格分开

#charset koi8-r; ## // 网页默认的编码格式

#access_log  logs/host.access.log  main; ## // 虚拟主机的日志访问路径 main 日志输入格式;

location / {

## //  URL 匹配特定位置的配置

## // location 支持正则表达式 也支持条件判断 也支持条件匹配

root   html; ## //  默认目录

index  index.html index.htm; ## //

}

#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;

}

九、 【  nginx 支持 PHP  】

server {

server_name www.www1.com www1.com;

location / {

index index.php  index.html;

root  /web/www/www1.com

}

location ~ \.php$ {

root           html;

fastcgi_pass   127.0.0.1:9000;

fastcgi_index  index.php;

fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

## // fastcgi_param 放置PHP动态程序的主目录,也是scripts$fastcgi_script_name 前面指定的目录;

include        fastcgi_params;

## // fastcgi 进程的参数配置文件;

}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /\.ht {

#    deny  all;

#}

}

【 nginx  中 FastCGI 参数;主要是在  http 层 ;保证PHP环境的高校运行;】 -- ## // 主要对PHP用来解析

fastcgi_cache_path  /tmp/fastcgi_cache  levels=1:2  keys_zone=TEST:10m  inactive=5m;

## //   fastcgi 缓存指定一个路径、目录结构、关键字缓存时间和非活动删除时间;

fastcgi_connect_timeout 300;

## //   指定连接到后端 Fastcgi 的超市时间

fastcgi_send_timeout 300;

## // 向 FastCGI 传送请求的超时时间 这个值是已经完成两次握手之后向FastCGI 传送请求的超时时间

fastcgi_read_timeout 300;

## //  指定接收FastCGI 应答的超时时间 也是两次握手之后的 接收FastCGI应答超时时间;

fastcgi_buffer_size 64k;

## // 读取FastCGI应答第一部分需要多大缓冲区大小;

fastcgi_buffer 4  16k;

## //  本地用多少和多大的缓存区来缓存FastCGI的应答请求;

fastcgi_busy_buffers_size 128k;

## //  默认是 fastcgi_buffer 的两倍 ;

fastcgi_temp_file_write_size 128k;

## //  指定在写入缓存文件时使用多大的数据块,默认也是 fastcgi_buffer 的两倍;

fastcgi_cache TEST;

## //   开启fastcgi_cache 缓存 并指定一个名称;  开启缓存可以有限降低CPU 的负载 ;并且放置502 的错误发生;

fastcgi_cahce_vaild   200 302 1h;

## //  指定应答代码的缓存时间; 200 301 应答缓存一个小时 ;

fastcgi_cache_vaild   301 1d;

## //  301 缓存设置 1 天;

fastcgi_cache_vaild   any 1m;

## //  其他缓存 设置 1分钟

# 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;

#    }

#}

}

十、 【 页面动静分离  后端 调用tomcat 集群 】

server {

listen 80;

server_name www.www2.com ;

root /web/www2/html

loation /img/ {

alias /web/www2/html/img/;

}

location ~ (\.jsp)|(\.do)$ {

proxy_pass http:## //serverip:port;

proxy_redirect off;

## // 重定向 指定主机名 ;

proxy_set_header Host $host;

## // set_header 设置后端服务器获取主机名或者真实IP;以及代理者的真实IP地址;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

client_body_buffer_size 128k;

## // 客户端请求主题的缓冲区大小 保存本地再传输到客户端;

proxy_connect_timeout 90;

## // 与后端服务器链接超时时间  即握手响应的超时时间;

proxy_read_timeout 90;

## // 设置nginx从代理的后端服务器获取信息的时间; 表示连接建立成功 nginx响应等待后端的时间;

proxy_send_timeout 90;

## // 后端服务器数据回传时间

proxy_buffer_size   4k;

## // 缓冲区大小

proxy_buffer     4 32k;

## // 缓冲区数量和大小 ;nginx 从后端服务器获取的响应信息;

proxy_busy_buffer_size   64k;

## // 系统很忙时 缓冲区的大小  一般是缓冲区设置的两倍;

proxy_temp_file_write_size  64k;

## // 指定proxy临时文件的大小 ;

}

}

upstream serverip {

## // 默认的是轮询;

sevrer 192.168.10.11:8080;

sevrer 192.168.10.12:8080;

sevrer 192.168.10.13:8080;

}