一.nginxs的三个依赖包
1.zlib库。 gzip 模块需要 zlib 库 ( 下载: http://www.zlib.net/ )
gzip(GNU-ZIP)是一种压缩技术。经过gzip压缩后页面大小可以变为原来的30%甚至更小,这样,用户浏览页面的时候速度会块得多。gzip 的压缩页面需要浏览器和服务器双方都支持,实际上就是服务器端压缩,传到浏览器后浏览器解压并解析。浏览器那里不需要我们担心,因为目前的巨大多数浏览器 都支持解析gzip过的页面。
Nginx的压缩输出有一组gzip压缩指令来实现。相关指令位于http{….}两个大括号之间。
默认情况下,Nginx的gzip压缩是关闭的, gzip压缩功能就是可以让你节省不少带宽,但是会增加服务器CPU的开销哦(与节约带宽相比,宁愿选择节约带宽,节约带宽就是快速响应用户的访问)。
Nginx默认只对text/html进行压缩 ,如果要对html之外的内容进行压缩传输,我们需要手动来调。
2.pcre库。 rewrite 模块需要 pcre 库 ( 下载: http://www.pcre.org/ )
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。这些在执行正则表达式模式匹配时用与Perl 5同样的语法和语义是很有用的。
3.openssl库。 ssl 功能需要 openssl 库 ( 下载: http://www.openssl.org/ )
默认情况下ssl模块并未被安装,如果要使用该模块则需要在编译时指定–with-http_ssl_module参数,安装模块依赖于OpenSSL库和一些引用文件,通常这些文件并不在同一个软件包中。通常这个文件名类似libssl-dev。
二.nginx安装
1.依赖环境安装
yum -y install gcc gcc-c++ zlib openssl-devel zlib-devel
2.安装pcre-devel库
wget http://ftp.exim.llorien.org/pcre/pcre-8.36.tar.gz -P /download/
cd /download/
tar xf pcre-8.36.tar.gz -C /usr/src
cd /usr/src/pcre-8.36
./configure --prefix=/usr/local/pcre --enable-utf8 --enable-jit
make && make install
3.安装openssl(根据需要判断是否安装)
yum -y install openssl*
4.安装nginx
wget http://nginx.org/download/nginx-1.8.1.tar.gz -P /download/
cd /download/
tar xf nginx-1.8.1.tar.gz
cd nginx-1.8.1
groupadd -g 1001 deamon
useradd -M -u 1001 -g deamon -s /sbin/nologin deamon
./configure --prefix=/usr/local/nginx --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/run/nginx/nginx.lock --user=daemon --group=daemon --with-pcre=/usr/src/pcre-8.36 --with-threads --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module
make && make install
--with-pcre后面跟的是pcre的解压路径
--prefix=/usr/local/nginx #指定nginx 的安装路径
--error-log-path=/var/log/nginx/error.log #指定nginx错误日志的路径
--http-log-path=/var/log/nginx/access.log #指定用户访问http时信息保存路径
--pid-path=/var/run/nginx/nginx.pid #指定nginx的PID路径
--lock-path=/var/run/nginx/nginx.lock
--user=daemon --group=daemon #指定nginx的用户和组
--with-pcre=/usr/src/pcre-8.36 --with-threads #为了支持rewrite重写功能,指定pcre解压路径
###########################附加常用选项--------------------------------
--with-http_stub_status_module:支持nginx状态查询
--with-http_ssl_module:支持https
--with-http_spdy_module:支持google的spdy,想了解请百度spdy,这个必须有ssl的支持
参数解释
三.nginx主配文件修改
配置说明:
配置文件位置:/usr/local/nginx/conf/nginx.conf
Nginx配置文件分为4个部分
1. main(全局设置)
2. server(主机设置)
3. upstream(负载均衡设置)
4. localtion(URL匹配特定位置的设置)
server继承main location继承server
upstream即不会继承其它设置也不会被继承.
#==================================一全局配置#========================
user user_00 users; #这个模块指令,指Nginx Worker 运用的用户和组,默认为nobody
worker_processes 8; #指定了要开启的进程数,每进程占用10M~12M的内存,建议和CPU的核心数量一样多的进程就行了。
error_log logs/error.log; #全局错误日志
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid; #:用来指定进程ID的存储位置.
#Specifies the value for maximum file descriptors that can be opened by this process.
#events 用来指定Nginx工作模式以及连接数上限
events {
use epoll; #使用epoll高效模式,适用于Linux,Unix使用kqueue
worker_connections 100000; #定义Ningx没个进程最大的连接数。默认为1024,受到文件句柄的约束。
}
worker_rlimit_nofile 100000; #打开的文件句柄数量最高为10万
#==================================二、HTTP配置========================
http {
include mime.types; #实现对配置文件所包含的文件设定
default_type application/octet-stream; #属于HTTP核心模块,默认设定为二进制流
server_tokens off; #禁止错误页面里显示nginx的版本号
# 定义日志处理的格式
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
# 定义它的hash表为128K
server_names_hash_bucket_size 128;
client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,一般一个请求头的大小不会超过1k
large_client_header_buffers 4 32k; #客户请求头缓冲大小 nginx默认会用client_header_buffer_size这个buffer来读取header值
client_max_body_size 8m; #设定通过nginx上传文件的大小
#sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为on。
#如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。
sendfile on;
tcp_nopush on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 60; #keepalive超时时间。连接保持活动时间超过这个,将被关闭掉
#===================重要位置============
fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间。
fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout 300; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
fastcgi_buffer_size 254k; #指定读取FastCGI应答第一部分需要用多大的缓冲区
fastcgi_buffers 16 256k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
fastcgi_busy_buffers_size 512k; #这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。
fastcgi_temp_file_write_size 512k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。
gzip on; #该指令用于开启或关闭gzip模块(on/off)
gzip_min_length 1k; #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取
gzip_buffers 4 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
gzip_http_version 1.0; #识别http的协议版本
gzip_comp_level 2; #gzip压缩比,1压缩比最小处理速度最快
#匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的
gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
gzip_vary on; #和http头有关系,加个vary头,给代理服务器用的
charset utf-8; #字符集为utf-8
access_log off; # 日常日志关闭
log_not_found off; # 日常日志关闭
error_page 400 403 405 408 /40x.html; # 错误返回页面
error_page 500 502 503 504 /50x.html; # 错误返回页面
#===================Server虚拟机配置保持默认============
server {
listen 80 default; #默认监听端口号为80
server_name _;
return 444;
}
#===================自定义虚拟机配置文件===========
include vhost/vhost.www.fanhougame.com;
}
nginx配置文件解析
主配虚拟Server配置文件如下:
server {
listen 80 ; #监听端口号
#域名为
server_name 10.0.0.201;
# 指定网站的目录
root /data/www/oa.com/www.fanhougame.com ;
# localtion模块指定网站首页名称
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
return 444;
}
}
#:返回的错误信息
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
#可以指定多个localtion进行不同的指令处理,这里是指定php的sock
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi-5313-web.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $http_host;
fastcgi_ignore_client_abort on;
}
#指定对网页图片格式进行缓存max表示10年,也可以是30d(天)
location ~ \.(swf|js|css|xml|gif|jpg|jpeg|png|bmp)$ {
error_log off;
access_log off;
#expires 30d;
expires max;
}
}
vhost/vhost.www.fanhougame.com
四.开放端口
#vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
将上述代码加入ssh服务(22端口)下面
启动与平滑重启
# cd /usr/local/services/nginx/sbin/
# ./nginx –t 检测配置文件是否有错误
# ./nginx 启动nginx
# ./nginx -s reload 平滑重启
本次配置:
1 user deamon;
2 worker_processes 8; #指定了要开启的进程数,每进程占用10M~12M的内存,建议和CPU的核心数量一样多的进程就行了。
3 error_log /var/log/nginx/error.log warn;
4 pid /var/run/nginx/nginx.pid;
5 #Specifies the value for maximum file descriptors that can be opened by this process.
6 #events 用来指定Nginx工作模式以及连接数上限
7 events {
8 use epoll; #使用epoll高效模式,适用于Linux,Unix使用kqueue
9 worker_connections 100000; #定义Ningx没个进程最大的连接数。默认为1024,受到文件句柄的约束。
10 }
11 worker_rlimit_nofile 100000; #打开的文件句柄数量最高为10万
12
13
14 http {
15 include mime.types;
16 default_type application/octet-stream;
17
18 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
19 '$status $body_bytes_sent "$http_referer" '
20 '"$http_user_agent" "$http_x_forwarded_for"';
21
22 access_log /var/log/nginx/access.log main;
23 sendfile on;
24 #tcp_nopush on;
25 #keepalive_timeout 0;
26 keepalive_timeout 65;
27 server_names_hash_bucket_size 128;
28 client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,一般一个请求头的大小不会超过1k
29 large_client_header_buffers 4 32k; #客户请求头缓冲大小 nginx默认会用client_header_buffer_size这个buffer来读取header值
30 client_max_body_size 8m; #设定通过nginx上传文件的大小
31
32
33 gzip on; #该指令用于开启或关闭gzip模块(on/off)
34 gzip_min_length 1k; #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取
35 gzip_buffers 4 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
36 gzip_http_version 1.0; #识别http的协议版本
37 gzip_comp_level 2; #gzip压缩比,1压缩比最小处理速度最快
38 #匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的
39 gzip_types text/plain application/x-javascript text/css application/xml text/javascript;
40 gzip_vary on; #和http头有关系,加个vary头,给代理服务器用的
41
42 charset utf-8; #字符集为utf-8
43
44 access_log off; # 日常日志关闭
45 log_not_found off; # 日常日志关闭
46
47 #fastcgi_temp_path /etc/nginx/tmp;
48 #fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
49 #fastcgi_cache_key " request_method request_uri";
50
51 fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间。
52 fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
53 fastcgi_read_timeout 300; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
54 fastcgi_buffer_size 254k; #指定读取FastCGI应答第一部分需要用多大的缓冲区
55 fastcgi_buffers 16 256k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
56 fastcgi_busy_buffers_size 512k; #这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。
57 fastcgi_temp_file_write_size 512k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。
58
59
60 server {
61 listen 80 ; #监听端口号
62 #域名为
63 server_name localhost;
64 # 指定网站的目录
65 root /www/;
66 # localtion模块指定网站首页名称
67 location / {
68 index index.php index.html index.htm;
69 if (!-e $request_filename) {
70 return 444;
71 }
72 }
73 location ~ \.php$ {
74 root /www;
75 #fastcgi_cache MYAPP;
76 #fastcgi_cache_valid 200 60m;
77 fastcgi_pass unix:/tmp/php-cgi.sock;
78 fastcgi_index index.php;
79 fastcgi_cache_valid 200 60m;
80 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
81 include fastcgi_params;
82 }
83 location /zabbix{
84 alias /www/zabbix;
85 }
86 #指定对网页图片格式进行缓存max表示10年,也可以是30d(天)
87 location ~ \.(swf|js|css|xml|gif|jpg|jpeg|png|bmp)$ {
88 error_log off;
89 access_log off;
90 #expires 30d;
91 expires max;
92 }
93 }
94 }
View Code