一.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 ; #指定了要开启的进程数,每进程占用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 ; #定义Ningx没个进程最大的连接数。默认为1024,受到文件句柄的约束。
}
worker_rlimit_nofile ; #打开的文件句柄数量最高为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 ;
client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,一般一个请求头的大小不会超过1k
large_client_header_buffers 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 ;
keepalive_timeout ; #keepalive超时时间。连接保持活动时间超过这个,将被关闭掉 #===================重要位置============
fastcgi_connect_timeout ; #指定连接到后端FastCGI的超时时间。
fastcgi_send_timeout ; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout ; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
fastcgi_buffer_size 254k; #指定读取FastCGI应答第一部分需要用多大的缓冲区
fastcgi_buffers 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 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
gzip_http_version 1.0; #识别http的协议版本
gzip_comp_level ; #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-; #字符集为utf- access_log off; # 日常日志关闭
log_not_found off; # 日常日志关闭 error_page /40x.html; # 错误返回页面
error_page /50x.html; # 错误返回页面
#===================Server虚拟机配置保持默认============
server {
listen default; #默认监听端口号为80
server_name _;
return ;
}
#===================自定义虚拟机配置文件===========
include vhost/vhost.www.fanhougame.com; }

nginx配置文件解析

主配虚拟Server配置文件如下:

    server {
listen ; #监听端口号
#域名为
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 ;
}
} #:返回的错误信息
error_page /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
} #可以指定多个localtion进行不同的指令处理,这里是指定php的sock
location ~ \.php$ {
fastcgi_pass unix:/tmp/php-cgi--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 平滑重启

本次配置:

 user  deamon;
worker_processes ; #指定了要开启的进程数,每进程占用10M~12M的内存,建议和CPU的核心数量一样多的进程就行了。
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx/nginx.pid;
#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 ; #定义Ningx没个进程最大的连接数。默认为1024,受到文件句柄的约束。
}
worker_rlimit_nofile ; #打开的文件句柄数量最高为10万 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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout ;
keepalive_timeout ;
server_names_hash_bucket_size ;
client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,一般一个请求头的大小不会超过1k
large_client_header_buffers 32k; #客户请求头缓冲大小 nginx默认会用client_header_buffer_size这个buffer来读取header值
client_max_body_size 8m; #设定通过nginx上传文件的大小 gzip on; #该指令用于开启或关闭gzip模块(on/off)
gzip_min_length 1k; #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获取
gzip_buffers 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
gzip_http_version 1.0; #识别http的协议版本
gzip_comp_level ; #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-; #字符集为utf- access_log off; # 日常日志关闭
log_not_found off; # 日常日志关闭 #fastcgi_temp_path /etc/nginx/tmp;
#fastcgi_cache_path /etc/nginx/cache levels=: keys_zone=MYAPP:100m inactive=60m;
#fastcgi_cache_key " request_method request_uri"; fastcgi_connect_timeout ; #指定连接到后端FastCGI的超时时间。
fastcgi_send_timeout ; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout ; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
fastcgi_buffer_size 254k; #指定读取FastCGI应答第一部分需要用多大的缓冲区
fastcgi_buffers 256k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
fastcgi_busy_buffers_size 512k; #这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍。
fastcgi_temp_file_write_size 512k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。 server {
listen ; #监听端口号
#域名为
server_name localhost;
# 指定网站的目录
root /www/;
# localtion模块指定网站首页名称
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
return ;
}
}
location ~ \.php$ {
root /www;
#fastcgi_cache MYAPP;
#fastcgi_cache_valid 60m;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
fastcgi_cache_valid 60m;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location /zabbix{
alias /www/zabbix;
}
#指定对网页图片格式进行缓存max表示10年,也可以是30d(天)
location ~ \.(swf|js|css|xml|gif|jpg|jpeg|png|bmp)$ {
error_log off;
access_log off;
#expires 30d;
expires max;
}
}
}

  

  

  

最新文章

  1. x01.Weiqi.8: 一点改进
  2. Sensor(GYROSCOPE)
  3. HDInsight 指定输出目录 insert overwrite
  4. kail-linux my need
  5. Android中Dialog
  6. Windows上搭建android开发环境
  7. 《OD大数据实战》Kafka入门实例
  8. DevExpress的JavaScript脚本智能提示
  9. linux —— 学习笔记(汇总)
  10. C++ 性能剖析 (三):Heap Object对比 Stack (auto) Object
  11. Linux gdb调试入门
  12. hdu 4735Little Wish~ lyrical step~ 重复覆盖
  13. Sublime Text 3设置笔记
  14. Python 安装matplotlib,six,dateutil,pyparsing 完整过程
  15. Tomcat8远程访问manager,host-manager被拒绝403
  16. RAP在线接口管理统计部署
  17. 如何在sublime text3运行nodejs
  18. Android为TV端助力:adb查找包名位置
  19. Android Studio项目用Git上传至码云(OSChina)
  20. 16.ajax_case01

热门文章

  1. Spark学习笔记——构建基于Spark的推荐引擎
  2. traff.sh
  3. Arduino基本数据类型
  4. hashlib模块configparser模块logging模块
  5. adb.exe,start-server' failed
  6. JVM源码分析之栈溢出完全解读
  7. Docker之OVS网络
  8. MFC实现一元稀疏多项式运算器
  9. spring @Order标记
  10. XPC connection interrupted