转自:https://www.cnblogs.com/bluestorm/p/4574688.htmljavascript
原文出处: Sean Chow(@SeanLoook) 欢迎分享原创到伯乐头条css
Nginx 在工做中已经有好几个环境在使用了,每次都是从新去网上找博客,各类编译配置,今天本身也整理一份安装文档和 nginx.conf 配置选项的说明,留做之后参考。html
咱们编译安装nginx来定制本身的模块,机器CentOS 6.2 x86_64。首先安装缺乏的依赖包:前端
1
|
# yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
|
这些软件包若是yum上没有的话能够下载源码来编译安装,只是要注意编译时默认安装的目录,确保下面在安装nginx时可以找到这些动态库文件(ldconfig)。java
从 http://nginx.org/en/download.html 下载稳定版nginx-1.6.3.tar.gz
到/usr/local/src
下解压。nginx
为了后续准备咱们另外下载2个插件模块:nginx_upstream_check_module-0.3.0.tar.gz —— 检查后端服务器的状态,nginx-goodies-nginx-sticky-module-ng-bd312d586752.tar.gz(建议在/usr/local/src下解压后将目录重命名为nginx-sticky-module-ng-1.2.5
) —— 后端作负载均衡解决session sticky问题(与upstream_check模块结合使用须要另外打补丁,请参考nginx负载均衡配置实战)。c++
请注意插件与nginx的版本兼容问题,通常插件越新越好,nginx不用追新,稳定第一。nginx-1.4.7,nginx-sticky-module-1.1,nginx_upstream_check_module-0.2.0,这个搭配也没问题。sticky-1.1与nginx-1.6版本因为更新没跟上编译出错。(能够直接使用Tengine,默认就包括了这些模块)git
1
2
3
4
5
6
7
8
|
[root@cachets nginx-1.6.3]
# pwd
/usr/local/src/nginx-1
.6.3
[root@cachets nginx-1.6.3]
# ./configure --prefix=/usr/local/nginx-1.6 --with-pcre \
> --with-http_stub_status_module --with-http_ssl_module \
> --with-http_gzip_static_module --with-http_realip_module \
> --add-module=..
/nginx_upstream_check_module-0
.3.0
[root@cachets nginx-1.6.3]
# make && make install
|
nginx大部分经常使用模块,编译时./configure --help
以--without
开头的都默认安装。github
--prefix=PATH
: 指定nginx的安装目录。默认 /usr/local/nginx
--conf-path=PATH
: 设置nginx.conf配置文件的路径。nginx容许使用不一样的配置文件启动,经过命令行中的-c选项。默认为prefix/conf/nginx.conf--user=name
: 设置nginx工做进程的用户。安装完成后,能够随时在nginx.conf配置文件更改user指令。默认的用户名是nobody。--group=name
相似--with-pcre
: 设置PCRE库的源码路径,若是已经过yum方式安装,使用--with-pcre
自动找到库文件。使用--with-pcre=PATH
时,须要从PCRE网站下载pcre库的源码(版本4.4 – 8.30)并解压,剩下的就交给Nginx的./configure
和make
来完成。perl正则表达式使用在location
指令和 ngx_http_rewrite_module
模块中。--with-zlib=PATH
: 指定 zlib(版本1.1.3 – 1.2.5)的源码解压目录。在默认就启用的网络传输压缩模块ngx_http_gzip_module
时须要使用zlib 。--with-http_ssl_module
: 使用https协议模块。默认状况下,该模块没有被构建。前提是openssl与openssl-devel已安装--with-http_stub_status_module
: 用来监控 Nginx 的当前状态--with-http_realip_module
: 经过这个模块容许咱们改变客户端请求头中客户端IP地址值(例如X-Real-IP 或 X-Forwarded-For),意义在于可以使得后台服务器记录原始客户端的IP地址--add-module=PATH
: 添加第三方外部模块,如nginx-sticky-module-ng或缓存模块。每次添加新的模块都要从新编译(Tengine能够在新加入module时无需从新编译)再提供一种编译方案:正则表达式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
.
/configure
\
> --prefix=
/usr
\
> --sbin-path=
/usr/sbin/nginx
\
> --conf-path=
/etc/nginx/nginx
.conf \
> --error-log-path=
/var/log/nginx/error
.log \
> --http-log-path=
/var/log/nginx/access
.log \
> --pid-path=
/var/run/nginx/nginx
.pid \
> --lock-path=
/var/lock/nginx
.lock \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --http-client-body-temp-path=
/var/tmp/nginx/client/
\
> --http-proxy-temp-path=
/var/tmp/nginx/proxy/
\
> --http-fastcgi-temp-path=
/var/tmp/nginx/fcgi/
\
> --http-uwsgi-temp-path=
/var/tmp/nginx/uwsgi
\
> --with-pcre=..
/pcre-7
.8
> --with-zlib=..
/zlib-1
.2.3
|
1
2
3
4
5
6
7
8
9
10
11
12
|
## 检查配置文件是否正确
# /usr/local/nginx-1.6/sbin/nginx -t
# ./sbin/nginx -V # 能够看到编译选项
## 启动、关闭
# ./sbin/nginx # 默认配置文件 conf/nginx.conf,-c 指定
# ./sbin/nginx -s stop
或 pkill nginx
## 重启,不会改变启动时指定的配置文件
# ./sbin/nginx -s reload
或 kill -HUP `cat /usr/local/nginx-1.6/logs/nginx.pid`
|
固然也能够将 nginx 做为系统服务管理,下载 nginx 到/etc/init.d/
,修改里面的路径而后赋予可执行权限。
1
|
# service nginx {start|stop|status|restart|reload|configtest}
|
—— 2015-05-22更新
yum安装rpm包会比编译安装简单不少,默认会安装许多模块,但缺点是若是你想之后安装第三方模块那就没办法了。
1
2
3
4
5
6
|
# vi /etc/yum.repo.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http:
//nginx
.org
/packages/centos/
$releasever/$basearch/
gpgcheck=0
enabled=1
|
剩下的就yum install nginx
搞定,也能够yum install nginx-1.6.3
安装指定版本(前提是你去packages里看到有对应的版本,默认是最新版稳定版)。
Nginx配置文件主要分红四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。main部分设置的指令将影响其它全部部分的设置;server部分的指令主要用于指定虚拟主机域名、IP和端口;upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;location部分用于匹配网页位置(好比,根目录“/”,“/images”,等等)。他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有本身的特殊指令,不须要在其余地方的应用。
当前nginx支持的几个指令上下文:
下面的nginx.conf
简单的实现nginx在前端作反向代理服务器的例子,处理js、png等静态文件,jsp等动态请求转发到其它服务器tomcat:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
user www www;
worker_processes 2;
error_log logs
/error
.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs
/nginx
.pid;
events {
use epoll;
worker_connections 2048;
}
http {
include mime.types;
default_type application
/octet-stream
;
#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;
sendfile on;
# tcp_nopush on;
keepalive_timeout 65;
# gzip压缩功能设置
gzip
on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text
/html
text
/plain
text
/css
text
/javascript
application
/json
application
/javascript
application
/x-javascript
application
/xml
;
gzip_vary on;
# 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 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 {
listen 80;
server_name itoatest.example.com;
root
/apps/oaapp
;
charset utf-8;
access_log logs
/host
.access.log main;
#对 / 全部作负载均衡+反向代理
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
;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root
/apps/oaapp
;
expires 7d;
}
location
/nginx_status
{
stub_status on;
access_log off;
allow 192.168.10.0
/24
;
deny all;
}
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;
}
}
## 其它虚拟主机,server 指令开始
}
|
nginx在运行时与具体业务功能(好比http服务或者email服务代理)无关的一些参数,好比工做进程数,运行的身份等。
woker_processes 2
grep ^processor /proc/cpuinfo | wc -l
,也是 auto 值,若是开启了ssl和gzip更应该设置成与逻辑CPU数量同样甚至为2倍,能够减小I/O操做。若是nginx服务器还有其它服务,能够考虑适当减小。worker_cpu_affinity
worker_cpu_affinity 0001 0010 0100 1000;
(四核)。worker_connections 2048
最大链接数 = worker_processes * worker_connections/4
,因此这里客户端最大链接数是1024,这个能够增到到8192都不要紧,看状况而定,但不能超事后面的worker_rlimit_nofile
。当nginx做为http服务器时,计算公式里面是除以2。worker_rlimit_nofile 10240
use epoll
events
部分。在Linux操做系统下,nginx默认使用epoll事件模型,得益于此,nginx在Linux操做系统下效率至关高。同时Nginx在OpenBSD或FreeBSD操做系统上采用相似于epoll的高效事件模型kqueue。在操做系统不支持这些高效模型时才使用select。与提供http服务相关的一些配置参数。例如:是否使用keepalive啊,是否使用gzip进行压缩等。
sendfile on
keepalive_timeout 65
: 长链接超时时间,单位是秒,这个参数很敏感,涉及浏览器的种类、后端服务器的超时设置、操做系统的设置,能够另外起一片文章了。长链接请求大量小文件的时候,能够减小重建链接的开销,但假若有大文件上传,65s内没上传完成会致使失败。若是设置时间过长,用户又多,长时间保持链接会占用大量资源。send_timeout
: 用于指定响应客户端的超时时间。这个超时仅限于两个链接活动之间的时间,若是超过这个时间,客户端没有任何活动,Nginx将会关闭链接。client_max_body_size 10m
client_body_buffer_size 128k
模块http_proxy:
这个模块实现的是nginx做为反向代理服务器的功能,包括缓存功能(另见文章)
proxy_connect_timeout 60
proxy_read_timeout 60
proxy_buffer_size 4k
proxy_buffers 4 32k
proxy_busy_buffers_size 64k
proxy_max_temp_file_size
proxy_temp_file_write_size 64k
proxy_temp_path
(能够在编译的时候)指定写到哪那个目录。proxy_pass,proxy_redirect见 location 部分。
模块http_gzip:
gzip on
: 开启gzip压缩输出,减小网络传输。
gzip_min_length 1k
: 设置容许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取。默认值是20。建议设置成大于1k的字节数,小于1k可能会越压越大。gzip_buffers 4 16k
: 设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流。4 16k表明以16k为单位,安装原始数据大小以16k为单位的4倍申请内存。gzip_http_version 1.0
: 用于识别 http 协议的版本,早期的浏览器不支持 Gzip 压缩,用户就会看到乱码,因此为了支持前期版本加上了这个选项,若是你用了 Nginx 的反向代理并指望也启用 Gzip 压缩的话,因为末端通讯是 http/1.0,故请设置为 1.0。gzip_comp_level 6
: gzip压缩比,1压缩比最小处理速度最快,9压缩比最大但处理速度最慢(传输快但比较消耗cpu)gzip_types
:匹配mime类型进行压缩,不管是否指定,”text/html”类型老是会被压缩的。gzip_proxied any
: Nginx做为反向代理的时候启用,决定开启或者关闭后端服务器返回的结果是否压缩,匹配的前提是后端服务器必需要返回包含”Via”的 header头。gzip_vary on
: 和http头有关系,会在响应头加个 Vary: Accept-Encoding ,可让前端的缓存服务器缓存通过gzip压缩的页面,例如,用Squid缓存通过Nginx压缩的数据。。http服务上支持若干虚拟主机。每一个虚拟主机一个对应的server配置项,配置项里面包含该虚拟主机相关的配置。在提供mail服务的代理时,也能够创建若干server。每一个server经过监听地址或端口来区分。
listen
listen *:80
、listen 127.0.0.1:80
等形式。server_name
localhost
、www.example.com
,能够经过正则匹配。模块http_stream
这个模块经过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡,upstream
后接负载均衡器的名字,后端realserver以 host:port options;
方式组织在 {} 中。若是后端被代理的只有一台,也能够直接写在 proxy_pass 。
http服务中,某些特定的URL对应的一系列配置项。
root /var/www/html
location
URL匹配的是子目录或文件,root
没什么做用,通常放在server
指令里面或/
下。index index.jsp index.html index.htm
root
放proxy_pass http:/backend
upstream
负载均衡器。也能够proxy_pass http://ip:port
。proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
关于location匹配规则的写法,能够说尤其关键且基础的,参考文章 nginx配置location总结及rewrite规则写法;
Nginx 的访问控制模块默认就会安装,并且写法也很是简单,能够分别有多个allow,deny,容许或禁止某个ip或ip段访问,依次知足任何一个规则就中止往下匹配。如:
1
2
3
4
5
6
7
8
9
10
|
location
/nginx-status
{
stub_status on;
access_log off;
# auth_basic "NginxStatus";
# auth_basic_user_file /usr/local/nginx-1.6/htpasswd;
allow 192.168.10.100;
allow 172.29.73.0
/24
;
deny all;
}
|
咱们也经常使用 httpd-devel 工具的 htpasswd 来为访问的路径设置登陆密码:
1
2
3
4
5
6
7
|
# htpasswd -c htpasswd admin
New
passwd
:
Re-
type
new password:
Adding password
for
user admin
# htpasswd htpasswd admin //修改admin密码
# htpasswd htpasswd sean //多添加一个认证用户
|
这样就生成了默认使用CRYPT加密的密码文件。打开上面nginx-status的两行注释,重启nginx生效。
Nginx默认是不容许列出整个目录的。如需此功能,打开nginx.conf文件,在location,server 或 http段中加入autoindex on;
,另外两个参数最好也加上去:
autoindex_exact_size off;
默认为on,显示出文件的确切大小,单位是bytes。改成off后,显示出文件的大概大小,单位是kB或者MB或者GBautoindex_localtime on;
1
2
3
4
5
6
|
location
/images
{
root
/var/www/nginx-default/images
;
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
}
|
参考