Nginx的几种经常使用配置

 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。  php

 Nginx 超越 Apache 的高性能和稳定性,使得国内使用 Nginx 做为 Web 服务器的网站也愈来愈多,其中包括新浪博客、新浪播客、网易新闻等门户网站频道,六间房、56.com等视频分享网站,Discuz!官方论坛、水木社区等知名论坛,豆瓣、YUPOO相册、海内SNS、迅雷在线等新兴Web 2.0网站。css

1、常见的502问题解决html

  1. nginx虚拟主机网站的配置文件里的fastcgi_pass使用的socket和php-fpm.conf配置文件里监听的socket文件名不一样。nginx

  2. php-fpm的线程用完了apache

  3. 使用的socket文件的属主和属组和跑nginx线程的属主属组不一样。vim

2、nginx用户认证bash

咱们对www.test.com/admin.php作一个用户认证服务器

vim /usr/local/nginx/conf/vhosts/test.conf   编辑虚拟主机的配置文件负载均衡

更改内容以下:红色部分是增长的部分curl

server

{

    listen 80;

    server_name www.test.com;

    index index.html index.htm index.php;

    root /data/www;

    location ~ .*admin\.php$ {

    auth_basic "cct auth";

    auth_basic_user_file /usr/local/nginx/conf/.htpasswd;

    include fastcgi_params;

    fastcgi_pass unix:/tmp/www.sock;

   #fastcgi_pass 127.0.0.1:9000;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

}

htpasswd -c /usr/local/nginx/conf/.htpasswd cct   创建密码文件,指定用户名cct和密码

注意的是,htpasswd工具要先安装apache,yum install -y httpd

3、域名跳转

vim /usr/local/nginx/conf/vhosts/test.conf

加入如下内容

server_name www.test.com www.aaa.com www.bbb.com;   #设定多个域名

    if ($host != 'www.test.com')

    {

       rewrite ^/(.*)$ http://www.test.com/$1 permanent;     #跳转规则  跳转的规则

    } 

4、不记录指定文件类型日志

vim /usr/local/nginx/conf/nginx.conf 

log_format cct '$remote_addr $http_x_forwarded_for [$time_local]'  #红字部分能够自定义

    '$host "$request_uri" $status'

    '"$http_referer" "$http_user_agent"';

在/usr/local/nginx/conf/vhosts/test.conf里加入

access_log /tmp/nginx_access.log cct;   红字对应

下列加入虚拟主机配置文件里

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$     #限定.gif等文件的日志

    {

        access_log off;

    }  

    location ~ (static|cache)            #限定带有static和cache的日志

    {

         access_log off;

     } 

5、日志切割

和apache不一样的是,nginx没有apache同样的工具作切割,须要编写脚本实现。

在/usr/local/sbin下写脚本

#!/bin/bash

dd=$(date -d '-1 day' +%F)

[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log

mv /tmp/nginx_access.log /tmp/nginx_log/$dd.log

/etc/init.d/nginx reload > /dev/null

6、配置静态文件过时时间

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$

    {

        access_log off;

        expires 15d;    #保存15天

    }

    location ~ \.(js|css)

    {

      access_log off;

      expires 2h;      #保存两个小时

    }

curl -x127.0.0.1:80 http://www.test.com/static/p_w_picpath/common/logo.png -I #测试图片的max-age

7、配置防盗链

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|bz2|gz)$

    {

        access_log off;

        expires 15d;

        valid_referers none blocked *.test.com *.aaa.com ;    #设置白名单

        if($invalid_referer)                        #其他的返回403

        {

           return 403;

         }

    }

curl -e "http://www.test.com/" -I -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo.png'  #返回200正常能够调用

curl -e "http://www.baidu.com/"  -I -x127.0.0.1:80 'http://www.test.com/static/p_w_picpath/common/logo.png'  返回403

curl -e指定referer时,要带上http://

8、nginx访问控制

能够设置一些配置禁止一些ip的访问

deny 127.0.0.1;     #全局定义限制,location里的是局部定义的。若是二者冲突,以location这种精确地优先,

location ~ .*admin\.php$ {

    #auth_basic "cct auth";

    #auth_basic_user_file /usr/local/nginx/conf/.htpasswd;

    allow 127.0.0.1;  只容许127.0.0.1的访问,其余均拒绝

    deny all;

    include fastcgi_params;

    fastcgi_pass unix:/tmp/www.sock;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }

当定义了匹配对象的时候是优先匹配。 也就是越精准越优先。当没有定义匹配对象的时候,是从上到下逐条匹配。

9、禁止指定user_agent

虚拟主机的配置文件里加入:

  if ($http_user_agent ~* 'baidu|360|sohu')       #禁止useragent为baidu、360和sohu,~*表示不区分大小写匹配

    {

       return 403;

    }

location /  和  location  ~ /  优先级是不同的。 

结合这个文章研究一下吧 http://blog.itpub.net/27181165/viewspace-777202/

curl -A "baidu" -x127.0.0.1:80 www.test.com/forum.php -I    该命令指定百度为user_agent,返回403

10、nginx代理

在vhosts目录下,vim proxy.conf

upstream cct{                    #自定义的

     server 111.13.100.91:80;       #对应www.baidu.com的两个ip地址

     server 111.13.100.92:80;

}

server{

    listen 80;

    server_name www.baidu.com;

    location / {

    proxy_pass http://cct/; #若只有一个IP地址,将cct替换成为IP地址,再将upstream模块去掉

    proxy_set_header Host $host;    #多个ip地址要把这一行加上,不然会出现502错误

    #proxy_set_header X-Real-IP $remote_addr;

    }

}

dig www.baidu.com 得到域名的多个IP地址

也能够用nginx作简单的负载均衡

相关文章
相关标签/搜索