nginx的配置
2017-08-27 本文已影响0人
逢场作戏_6fb9
1 server { ... } 配置一个虚拟主机;
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
在/etc/nginx/conf.d/vhost.conf中定义一个虚拟主机

在物理主机中添加nds解析后访问该域名,默认为80端口,注意清空防火墙策略

2 ngx_http_access_module模块,基于ip的访问控制模块

重载nginx的配置文件后访问


3.ngx_http_auth_basic_module模块实现基于用户的访问控制,使用basic机制进行用户认证
实现该功能模块前需安装httpd-tools包,需要使用htpasswd工具。

htpasswd -c -m /etc/nginx/.ngxpasswd kbq-------定义一个kbq用户可访问,使用md5加密,存放于.ngxpasswd文件中。

4.ngx_http_stub_status_module模块;用于输出nginx的基本状态信息


显示简述
Active connections: 活动状态的连接数;
accepts:已经接受的客户端请求的总数;
handled:已经处理完成的客户端请求的总数;
requests:客户端发来的总的请求数;
Reading:处于读取客户端请求报文首部的连接的连接数;
Writing:处于向客户端发送响应报文过程中的连接数;
Waiting:处于等待客户端发出请求的空闲连接数;
5.ngx_http_log_module模块,必须在1.10版本之后才能使用

6.ngx_http_gzip_module:
The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.
1、gzip on | off;
Enables or disables gzipping of responses.
2、gzip_comp_level level;
Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.
3、 gzip_disable regex ...;
Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.
4、 gzip_min_length length;
启用压缩功能的响应报文大小阈值;
5、gzip_buffers number size;
支持实现压缩功能时为其配置的缓冲区数量及每个缓存区的大小;
6、gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
nginx作为代理服务器接收到从被代理服务器发送的响应报文后,在何种条件下启用压缩功能的;
off:对代理的请求不启用
no-cache, no-store,private:表示从被代理服务器收到的响应报文首部的Cache-Control的值为此三者中任何一个,则启用压缩功能;
7、gzip_types mime-type ...;
压缩过滤器,仅对此处设定的MIME类型的内容启用压缩功能;
示例:
gzip on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_proxied any;
gzip_types text/xml text/css application/javascript;
7.ngx_http_ssl_module模块


8.ngx_http_rewrite_module模块:将用户请求的URI基于regex所描述的模式进行检查,而后完成替换;


自动跳转为:

1、rewrite regex replacement [flag]
将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI;
注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,因此,隐含有循环机制;[flag]所表示的标志位用于控制此循环机制;
如果replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端;
301:永久重定向;
[flag]:
last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环;
break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环;
redirect:重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;不能以http://或https://开头;
permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;
##
2、return
return code [text];
return code URL;
return URL;
Stops processing and returns the specified code to a client.
3、 rewrite_log on | off;
是否开启重写日志;
4、 if (condition) { ... }
引入一个新的配置上下文 ;条件满足时,执行配置块中的配置指令;server, location;
condition:
比较操作符:
==
!=
~:模式匹配,区分字符大小写;
~*:模式匹配,不区分字符大小写;
!~:模式不匹配,区分字符大小写;
!~*:模式不匹配,不区分字符大小写;
文件及目录存在性判断:
-e, !-e
-f, !-f
-d, !-d
-x, !-x
5、set $variable value;
用户自定义变量 ;
9.ngx_http_referer_module模块
The ngx_http_referer_module module is used to block access to a site for requests with invalid values in the “Referer” header field.
1、valid_referers none | blocked | server_names | string ...;
定义referer首部的合法可用值;
none:请求报文首部没有referer首部;
blocked:请求报文的referer首部没有值;
server_names:参数,其可以有值作为主机名或主机名模式;
arbitrary_string:直接字符串,但可使用*作通配符;
regular expression:被指定的正则表达式模式匹配到的字符串;要使用~打头,例如 ~.*\.magedu\.com;
配置示例:
valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;
if($invalid_referer) {
return http://www.magedu.com/invalid.jpg;
}
10.ngx_http_proxy_module模块:

proxy_pass只能置于lication中



proxy_cache_path:

11.ngx_http_headers_module模块
The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;
1、add_header name value [always];
添加自定义首部;
add_header X-Via $server_addr;
add_header X-Accel $server_name;
2、expires [modified] time;
expires epoch | max | off;
用于定义Expire或Cache-Control首部的值;
-
PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.
location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; }
设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
禁止访问 .htxxx 文件
location ~ /\.ht {
deny all;
}
}
}