在centos中配置nginx+php的环境

环境版本:centos6.8/nginx1.10.2/php5.6.29

====================安装nginx1.10.2====================

#根据centos版本选择对应yum源

rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

#直接用yum安装

yum install nginx

====================安装php5.6.29====================

#php的官方下载地址是:http://www.php.net/downloads.php

wget http://cn2.php.net/distributions/php-5.6.29.tar.bz2

 #解压php压缩包

tar -xjf php-5.6.29.tar.bz2

 #进入php目录

cd php-5.6.29

#编译参数详解

--prefix=/usr/local/php#指定php安装目录
--with-apxs2=/usr/local/apache/bin/apxs
#整合apache,apxs功能是使用mod_so中的LoadModule指令,加载指定模块到apache,要求apache要打开SO模块,本例使用nginx所以不需要
--with-config-file-path=/usr/local/php/etc               
#指定php.ini位置
--with-MySQL=/usr/local/mysql
#mysql安装目录,对mysql的支持
--with-mysqli=/usr/local/mysql/bin/mysql_config
#mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。 
--enable-safe-mode#打开安全模式--enable-ftp#打开ftp的支持--enable-zip#打开对zip的支持
--with-bz2#打开对bz2文件的支持
--with-jpeg-dir#打开对jpeg图片的支持
--with-png-dir#打开对png图片的支持
--with-freetype-dir#打开对freetype字体库的支持
--without-iconv#关闭iconv函数,种字符集间的转换
--with-libXML-dir#打开libxml2库的支持
--with-XMLrpc#打开xml-rpc的c语言
--with-zlib-dir#打开zlib库的支持
--with-gd#打开gd库的支持
--enable-gd-native-ttf#支持TrueType字符串函数库
--with-curl#打开curl浏览工具的支持
--with-curlwrappers#运用curl工具打开url流
--with-ttf#打开freetype1.*的支持,可以不加了
--with-xsl#打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件
--with-gettext#打开gnu 的gettext 支持,编码库用到
--with-pear#打开pear命令的支持,PHP扩展用的
--enable-calendar#打开日历扩展功能
--enable-mbstring#多字节,字符串的支持
--enable-bcmath#打开图片大小调整,用到zabbix监控的时候用到了这个模块
--enable-sockets#打开 sockets 支持
--enable-exif#图片的元数据支持
--enable-magic-quotes#魔术引用的支持
--disable-rpath#关闭额外的运行库文件
--disable-debug#关闭调试模式
--with-mime-magic=/usr/share/file/magic.mime#魔术头文件位置
#CGI方式安装才用的参数
--enable-fpm                      
#打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序
--enable-fastCGI                  
#支持fastcgi方式启动PHP
--enable-force-CGI-redirect        
#重定向方式启动PHP
--with-ncurses                    
#支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库
--enable-pcntl#freeTDS需要用到的,可能是链接mssql才用到

#mhash和mcrypt算法的扩展
--with-mcrypt
--with-mhash
#以上函数库需要安装
--with-gmp#应该是支持一种规范
--enable-inline-optimization#优化线程
--with-openssl#openssl的支持,加密传输时用到的
--enable-dbase#建立DBA 作为共享模块
--with-pcre-dir=/usr/local/bin/pcre-config#perl的正则库案安装位置
--disable-dmalloc
--with-gdbm#dba的gdbm支持
--enable-sigchild
--enable-sysvsem
--enable-sysvshm
--enable-zend-multibyte#支持zend的多字节
--enable-mbregex
--enable-wddx
--enable-shmop
--enable-soap

#本例中精简安装,命令如下

yum install gcc libxml2 libxml2-devel
./configure --prefix=/usr/local/php --enable-fpm --enable-mysqlnd --with-pdo-mysql=mysqlnd
make && make install

#配置php.ini文件

cp php.ini-production /usr/local/php/lib/php.ini

增加mysql的支持

vi /usr/local/php/lib/php.ini

保存,重启
/etc/init.d/php-fpm restart

#配置php-fpm.conf文件

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
#pid设置,默认在安装目录中的var/run/php-fpm.pid,建议开启
 
error_log = log/php-fpm.log
#错误日志,默认在安装目录中的var/log/php-fpm.log
 
log_level = notice
#错误级别. 可用级别为: alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
 
emergency_restart_threshold = 60
emergency_restart_interval = 60s
#表示在emergency_restart_interval所设值内出现SIGSEGV或者SIGBUS错误的php-cgi进程数如果超过 emergency_restart_threshold个,php-fpm就会优雅重启。这两个选项一般保持默认值。
 
process_control_timeout = 0
#设置子进程接受主进程复用信号的超时时间. 可用单位: s(秒), m(分), h(小时), 或者 d(天) 默认单位: s(秒). 默认值: 0.
 
daemonize = yes
#后台执行fpm,默认值为yes,如果为了调试可以改为no。在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。
 
listen = 127.0.0.1:9000
#fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: 'ip:port', 'port', '/path/to/unix/socket'. 每个进程池都需要设置.
 
listen.backlog = -1
#backlog数,-1表示无限制,由操作系统决定,此行注释掉就行。backlog含义参考:http://www.3gyou.cc/?p=41
 
listen.allowed_clients = 127.0.0.1
#允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔. 如果没有设置或者为空,则允许任何服务器请求连接
 
listen.owner = www
listen.group = www
listen.mode = 0666
#unix socket设置选项,如果使用tcp方式访问,这里注释即可。
 
user = www
group = www
#启动进程的帐户和组
 
pm = dynamic #对于专用服务器,pm可以设置为static。
#如何控制子进程,选项有static和dynamic。如果选择static,则由pm.max_children指定固定的子进程数。如果选择dynamic,则由下开参数决定:
pm.max_children #,子进程最大数
pm.start_servers #,启动时的进程数
pm.min_spare_servers #,保证空闲进程数最小值,如果空闲进程小于此值,则创建新的子进程
pm.max_spare_servers #,保证空闲进程数最大值,如果空闲进程大于此值,此进行清理
 
pm.max_requests = 1000
#设置每个子进程重生之前服务的请求数. 对于可能存在内存泄漏的第三方模块来说是非常有用的. 如果设置为 '0' 则一直接受请求. 等同于 PHP_FCGI_MAX_REQUESTS 环境变量. 默认值: 0.
 
pm.status_path = /status
#FPM状态页面的网址. 如果没有设置, 则无法访问状态页面. 默认值: none. munin监控会使用到
 
ping.path = /ping
#FPM监控页面的ping网址. 如果没有设置, 则无法访问ping页面. 该页面用于外部检测FPM是否存活并且可以响应请求. 请注意必须以斜线开头 (/)。
 
ping.response = pong
#用于定义ping请求的返回相应. 返回为 HTTP 200 的 text/plain 格式文本. 默认值: pong.
 
request_terminate_timeout = 0
#设置单个请求的超时中止时间. 该选项可能会对php.ini设置中的'max_execution_time'因为某些特殊原因没有中止运行的脚本有用. 设置为 '0' 表示 'Off'.当经常出现502错误时可以尝试更改此选项。
 
request_slowlog_timeout = 10s
#当一个请求该设置的超时时间后,就会将对应的PHP调用堆栈信息完整写入到慢日志中. 设置为 '0' 表示 'Off'
 
slowlog = log/$pool.log.slow
#慢请求的记录日志,配合request_slowlog_timeout使用
 
rlimit_files = 1024
#设置文件打开描述符的rlimit限制. 默认值: 系统定义值默认可打开句柄是1024,可使用 ulimit -n查看,ulimit -n 2048修改。
 
rlimit_core = 0
#设置核心rlimit最大限制值. 可用值: 'unlimited' 、0或者正整数. 默认值: 系统定义值.
 
chroot =
#启动时的Chroot目录. 所定义的目录需要是绝对路径. 如果没有设置, 则chroot不被使用.
 
chdir =
#设置启动目录,启动时会自动Chdir到该目录. 所定义的目录需要是绝对路径. 默认值: 当前目录,或者/目录(chroot时)
 
catch_workers_output = yes
#重定向运行过程中的stdout和stderr到主要的错误日志文件中. 如果没有设置, stdout 和 stderr 将会根据FastCGI的规则被重定向到 /dev/null . 默认值: 空.

#配置php-fpm启动文件

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
/etc/init.d/php-fpm start

#查看cgi启动状态

netstat -tunlp |grep 9000

#如果存在端口冲突,需要修改php-fpm的监听端口

vi /usr/local/php/etc/php-fpm.conf

将listen = 127.0.0.1:9000修改为listen = 127.0.0.1:9100

再使用/etc/init.d/php-fpm start就可以启动正常了

====================配置nginx和php相关====================

vi /usr/local/nginx/conf/nginx.conf

  在最后加入下面的配置

server {
    listen       80;
    server_name  192.168.1.234;
    
    root   /var/www;
    index  index.html index.htm index.php;
    
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

如果想将网站的端口设置为81,修改了9000端口为9100时,配置文件修改为

vi /usr/local/nginx/conf/nginx.conf

 在最后加入下面的配置

server {
    listen       81;
    server_name  127.0.0.1;
    
    root   /root/php_web;
    index  index.html index.htm index.php;
    
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9100;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

重载nginx配置文件

/usr/local/nginx/sbin/nginx -s reload

#注意如果403了,请检查是否关闭selinux

====================nginx与pathinfo与rewrite====================

#如果是thinkphp之类的,会牵扯pathinfo,nginx默认不支持,配置如下:

location / {
    if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php?s=$1 last;
        break;
    }
}

====================关于nginx.conf的配置详解====================

user nginx nginx ;
Nginx用户及用户组。window下不指定

worker_processes 8;
工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。查看cpu核心数命令cat /proc/cpuinfo| grep "cpu cores"| uniq

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;
错误日志:存放路径。

pid logs/nginx.pid;
pid(进程标识符):存放路径。

worker_rlimit_nofile 204800;
指定进程可以打开的最大描述符:数目。
这个指令是指当一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(ulimit -n)与nginx进程数相除,但是nginx分配请求并不是那么均匀,所以最好与ulimit -n 的值保持一致。

现在在Linux 2.6内核下开启文件打开数为65535,worker_rlimit_nofile就相应应该填写65535。
这是因为nginx调度时分配请求到进程并不是那么的均衡,所以假如填写10240,总并发量达到3-4万时就有进程可能超过10240了,这时会返回502错误。

events
{
use epoll;
使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
补充说明:
与apache相类,nginx针对不同的操作系统,有不同的事件模型
A)标准事件模型
Select、poll属于标准事件模型,如果当前系统不存在更有效的方法,nginx会选择select或poll
B)高效事件模型
Kqueue:使用于FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 和 MacOS X.使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃。
Epoll:使用于Linux内核2.6版本及以后的系统。
/dev/poll:使用于Solaris 7 11/99+,HP/UX 11.22+ (eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+。
Eventport:使用于Solaris 10。 为了防止出现内核崩溃的问题, 有必要安装安全补丁。

worker_connections 204800;
没个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections

keepalive_timeout 60;
keepalive超时时间。

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求头的大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。
分页大小可以用命令getconf PAGESIZE 取得。
但也有client_header_buffer_size超过4k的情况,但是client_header_buffer_size该值必须设置为“系统分页大小”的整倍数。

open_file_cache max=65535 inactive=60s;
这个将为打开文件指定缓存,默认是没有启用的,max指定缓存数量,建议和打开文件数一致,inactive是指经过多长时间文件没被请求后删除缓存。

open_file_cache_valid 80s;
这个是指多长时间检查一次缓存的有效信息。

open_file_cache_min_uses 1;
open_file_cache指令中的inactive参数时间内文件的最少使用次数,如果超过这个数字,文件描述符一直是在缓存中打开的,如上例,如果有一个文件在inactive时间内一次没被使用,它将被移除。
}

##设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
include mime.types;
设定mime类型,类型由mime.type文件定义

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"';
log_format log404 '$status [$time_local] $remote_addr $host$request_uri $sent_http_location';
日志格式设置。
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址;
$remote_user:用来记录客户端用户名称;
$time_local: 用来记录访问时间与时区;
$request: 用来记录请求的url与http协议;
$status: 用来记录请求状态;成功是200,
$body_bytes_sent :记录发送给客户端文件主体内容大小;
$http_referer:用来记录从那个页面链接访问过来的;
$http_user_agent:记录客户浏览器的相关信息;
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址。

access_log  logs/host.access.log  main;
access_log  logs/host.access.404.log  log404;
用了log_format指令设置了日志格式之后,需要用access_log指令指定日志文件的存放路径;

server_names_hash_bucket_size 128;
#保存服务器名字的hash表是由指令server_names_hash_max_size 和server_names_hash_bucket_size所控制的。参数hash bucket size总是等于hash表的大小,并且是一路处理器缓存大小的倍数。在减少了在内存中的存取次数后,使在处理器中加速查找hash表键值成为可能。如果hash bucket size等于一路处理器缓存的大小,那么在查找键的时候,最坏的情况下在内存中查找的次数为2。第一次是确定存储单元的地址,第二次是在存储单元中查找键 值。因此,如果Nginx给出需要增大hash max size 或 hash bucket size的提示,那么首要的是增大前一个参数的大小.

client_header_buffer_size 4k;
客户端请求头部的缓冲区大小。这个可以根据你的系统分页大小来设置,一般一个请求的头部大小不会超过1k,不过由于一般系统分页都要大于1k,所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。

large_client_header_buffers 8 128k;
客户请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,如果

header过大,它会使用large_client_header_buffers来读取。

open_file_cache max=102400 inactive=20s;
这个指令指定缓存是否启用。

open_file_cache_valid 30s; 
open_file_cache_min_uses 2; 
open_file_cache_errors on;

open_file_cache_errors语法:open_file_cache_errors on | off 默认值:open_file_cache_errors off 使用字段:http, server, location 这个指令指定是否在搜索一个文件是记录cache错误.

open_file_cache_min_uses语法:open_file_cache_min_uses number 默认值:open_file_cache_min_uses 1 使用字段:http, server, location 这个指令指定了在open_file_cache指令无效的参数中一定的时间范围内可以使用的最小文件数,如果使用更大的值,文件描述符在cache中总是打开状态.

open_file_cache_valid语法:open_file_cache_valid time 默认值:open_file_cache_valid 60 使用字段:http, server, location 这个指令指定了何时需要检查open_file_cache中缓存项目的有效信息.

client_max_body_size 300m;
设定通过nginx上传文件的大小

sendfile on;
sendfile指令指定 nginx 是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度,降低系统uptime。

tcp_nopush on;
此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用

proxy_connect_timeout 90; 
后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout 180;
连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间)

proxy_send_timeout 180;
后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据

proxy_buffer_size 256k;
设置从被代理服务器读取的第一部分应答的缓冲区大小,通常情况下这部分应答中包含一个小的应答头,默认情况下这个值的大小为指令proxy_buffers中指定的一个缓冲区的大小,不过可以将其设置为更小

proxy_buffers 4 256k;
设置用于读取应答(来自被代理服务器)的缓冲区数目和大小,默认情况也为分页大小,根据操作系统的不同可能是4k或者8k

proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
设置在写入proxy_temp_path时数据的大小,预防一个工作进程在传递文件时阻塞太长

proxy_temp_path /data0/proxy_temp_dir;
proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#设置内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。

keepalive_timeout 120;
keepalive超时时间。

tcp_nodelay on;

client_body_buffer_size 512k;
如果把它设置为比较大的数值,例如256k,那么,无论使用firefox还是IE浏览器,来提交任意小于256k的图片,都很正常。如果注释该指令,使用默认的client_body_buffer_size设置,也就是操作系统页面大小的两倍,8k或者16k,问题就出现了。
无论使用firefox4.0还是IE8.0,提交一个比较大,200k左右的图片,都返回500 Internal Server Error错误

proxy_intercept_errors on;
表示使nginx阻止HTTP应答代码为400或者更高的应答。

 upstream bakend {
server 127.0.0.1:8027;
server 127.0.0.1:8028;
server 127.0.0.1:8029;
hash $request_uri;
}

nginx的upstream目前支持4种方式的分配
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}

2、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream bakend {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}

3、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream backend {
server server1;
server server2;
fair;
}

4、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream backend {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}

tips:
upstream bakend{#定义负载均衡设备的Ip及设备状态}{
ip_hash;
server 127.0.0.1:9090 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://bakend/;

每个设备的状态设置为:
1.down表示单前的server暂时不参与负载
2.weight为weight越大,负载的权重就越大。
3.max_fails:允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path设置记录文件的目录 可以设置最多3层目录
location对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

##配置虚拟机
server
{
listen 80;
配置监听端口
server_name image.***.com;
配置访问域名

location ~* \.(mp3|exe)$ {
对以“mp3或exe”结尾的地址进行负载均衡
proxy_pass http://img_relay$request_uri;
设置被代理服务器的端口或套接字,以及URL
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 /face {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
error_page 404 502 = @fetch;
}

location @fetch {
access_log /data/logs/face.log log404;
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}

location /image {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
error_page 404 502 = @fetch;
}

location @fetch {
access_log /data/logs/image.log log404;
rewrite ^(.*)$ http://211.151.188.190:8080/face.jpg redirect;
}
}

##其他举例
server
{
listen 80;
server_name *.***.com *.***.cn;
location ~* \.(mp3|exe)$ {
proxy_pass http://img_relay$request_uri;
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 / {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#error_page 404 http://i1.***img.com/help/noimg.gif;
error_page 404 502 = @fetch;
}

location @fetch {
access_log /data/logs/baijiaqi.log log404;
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
}
server
{
listen 80;
server_name *.***img.com;
location ~* \.(mp3|exe)$ {
proxy_pass http://img_relay$request_uri;
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 / {
if ($http_user_agent ~* "xnp") {
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif;
}
proxy_pass http://img_relay$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#error_page 404 http://i1.***img.com/help/noimg.gif;
error_page 404 = @fetch;
}
#access_log off;

location @fetch {
access_log /data/logs/baijiaqi.log log404;
rewrite ^(.*)$ http://i1.***img.com/help/noimg.gif redirect;
}
}

server
{
listen 8080;
server_name ngx-ha.***img.com;
location / {
stub_status on;
access_log off;
}
}

server {
listen 80;
server_name imgsrc1.***.net;
root html;
}


server {
listen 80;
server_name ***.com w.***.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.***.com/ ;
}
}

server {
listen 80;
server_name *******.com w.*******.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.*******.com/;
}
}

server {
listen 80;
server_name ******.com;
# access_log /usr/local/nginx/logs/access_log main;
location / {
rewrite ^(.*)$ http://www.******.com/;
}
}

location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
#设定查看Nginx状态的地址

location ~ /\.ht {
deny all;
}
#禁止访问.htxxx文件
}

注释:变量
Ngx_http_core_module模块支持内置变量,他们的名字和apache的内置变量是一致的。
首先是说明客户请求title中的行,例如$http_user_agent,$http_cookie等等。
此外还有其它的一些变量
$args此变量与请求行中的参数相等
$content_length等于请求行的“Content_Length”的值。
$content_type等同与请求头部的”Content_Type”的值
$document_root等同于当前请求的root指令指定的值
$document_uri与$uri一样
$host与请求头部中“Host”行指定的值或是request到达的server的名字(没有Host行)一样
$limit_rate允许限制的连接速率
$request_method等同于request的method,通常是“GET”或“POST”
$remote_addr客户端ip
$remote_port客户端port
$remote_user等同于用户名,由ngx_http_auth_basic_module认证
$request_filename当前请求的文件的路径名,由root或alias和URI request组合而成
$request_body_file
$request_uri含有参数的完整的初始URI
$query_string与$args一样
$sheeme http模式(http,https)尽在要求是评估例如
Rewrite ^(.+)$ $sheme://example.com$; Redirect;
$server_protocol等同于request的协议,使用“HTTP/或“HTTP/
$server_addr request到达的server的ip,一般获得此变量的值的目的是进行系统调用。为了避免系统调用,有必要在listen指令中指明ip,并使用bind参数。
$server_name请求到达的服务器名
$server_port请求到达的服务器的端口号
$uri等同于当前request中的URI,可不同于初始值,例如内部重定向时或使用index

====================其他优化配置====================

#调整vim tab键为4个空格

vim /etc/vimrc 
set ts=4
set expandtab
set autoindent

#selinux关闭

getenforce
#查看selinux状态
vim /etc/selinux/config 
SELINUX=disabled

#修改ulimit

cat /proc/sys/fs/file-max
#查看系统最大限制
vim /etc/security/limits.conf
* soft nofile 32768
* hard nofile 65536
ulimit -n 65535
#临时修改,重启无效

猜你喜欢

转载自blog.csdn.net/tiging/article/details/131360975