876543e 发表于 2017-1-10 12:51:14

nginx 高级配置示例

一、用户认证
用户认证功能是利用Apache的工具htpasswd生成的密钥,所以需要安装Apache的这个工具即可,我们用yum来安装就可以。
# yum install -y httpd
# htpasswd -c /usr/local/nginx/conf/.htpasswd test
New password:
Re-type new password:    ##创建第二个账户密码,不需要-c
Adding password for user mydiscuz
# cat /usr/local/nginx/conf/.htpasswd
mydiscuz:$apr1$ejPLa15T$kuyykf8at2I77oogZ0kUz1
    修改配置,主要是修改server模块
location ~ .*admin\.php$ {
      auth_basic "testlinux auth";
      auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
      include fastcgi_params;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
      }

#/usr/local/nginx/sbin/nginx -t   检查
# service nginx reload   重新加载

二、域名重定向(域名跳转)
cd /usr/local/nginx/conf/vhosts/--> vim test.conf插入
server
{
    listen 80;
    server_name www.test.com www.aaa.com www.bbb.com;
    if ($host   !=   'www.test.com')
    {
      rewrite   ^/(.*)$   http://www.test.com/$1   permanent;
   }
    index index.html index.htm index.php;
    root /data/www;
    location ~ .*admin\.php$ {
以下无改动
2. /usr/local/nginx/sbin/nginx -t检查。 /usr/local/nginx/sbin/nginx -s reload 重新加载
3.在C:\Windows\System32\drivers\etc找到hosts用文本打开在
# localhost name resolution is handled within DNS itself.
#      127.0.0.1       localhost
#      ::1             localhost
192.168.52.120www.test.comwww.aaa.comwww.222.comwww.bbb.com

三、静态文件缓存与不记录指定文件日志
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
   expires      30d;
   access_log off;
   }

location ~ .*\.(js|css)?$
    {
   expires      12h;
   access_log off;
   }

四、防盗链
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|rar|zip|gz|bz2)$      
#针对这些文件进行防盗链配置
    {
      access_log off;
      expires 15d;
      valid_referers none blocked*.test.com *.aaa.com *.bbb.com;#只允许这几个域名
      if ($invalid_referer)
      {
             return 403;
#       rewrite ^/ http://www.example.com/nophoto.gif;   ##可以跳转到某图片
      }
    }



/usr/local/nginx/sbin/nginx-t检验   /usr/local/nginx/sbin/nginx -sreload重启

五、设置日志记录的内容
1. vim /usr/local/nginx/conf/nginx.conf做出如下更改
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format test'$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    sendfile on;
2.vim test.conf配置文件
    index index.html index.htm index.php;
    root /data/www;
    access_log /tmp/access.log test;   自定义地址及名字最好是比较大的一个磁盘下面

六 、日志切割
因为Nginx没有自动切割日志功能,所以需要手动编辑脚本
1.脚本实现
#vim/usr/local/sbin/nginx_log.sh
nginx日志按日期自动切割脚本如下:
#!/bin/bash
d=`date -d "-1 day" +%F`                                                    #定义切割脚本的格式
[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log         #检查此目录,如果没有就新建
mv /tmp/access.log /tmp/nginx_log/$d.log            #移动切割后的日志到新的目录下
/etc/init.d/nginx reload > /dev/null                                    
cd /tmp/nginx_log/
gzip -f $d.log                           #压缩切割后的额脚本
2.定时工作

在crontab中设置作业
#crontab -e
0 0 * * * bash /usr/local/sbin/nginx_log.sh      

七、访问控制
   1. 限制只让某个ip访问
    allow          219.232.244.234;
    deny         all;

2.禁止某个IP或者IP段访问站点的设置方法
首先建立下面的配置文件放在nginx的conf目录下面,命名为deny.ip   
catdeny.ip
deny 192.168.1.11;
deny 192.168.1.123;
deny 10.0.1.0/24;

在nginx的配置文件nginx.conf中加入:
include deny.ip;

重启一下nginx的服务:/usr/local/nginx/sbin/nginxreload 就可以生效了。

deny.ip 的格式中也可以用deny all;
如果你想实现这样的应用,除了几个IP外,其他全部拒绝,
那需要你在deny.ip 中这样写
allow 1.1.1.1;
allow 1.1.1.2;
deny all;

3.有时候会根据目录来限制php解析:
location ~ .*(diy|template|attachments|forumdata|attachment|image|admin)/.*\.php$
{
      allow 127.0.0.1;   
         deny all;
}


八、使用 user_agent 控制客户端访问
location /
{
    if ($http_user_agent ~ 'bingbot/2.0|MJ12bot/v1.4.2|Spider/3.0|YoudaoBot|Tomato|Gecko/20100315'){
            return 403;
    }
}

九、Nginx代理
1.绑定多个地址:yum install bind*先安装-->dig www.baidu.com查询到多个地址
#cd    /usr/local/nginx/conf/vhosts
#vimproxy.conf编辑如下:

upstream test{
    server61.135.169.125:80;
    server61.135.169.121:80;
}
server {
    listen 80;
    server_name www.baidu.com;

    location / {
      proxy_pass http://test/;
      proxy_set_header Host $host;
    }
}
2.绑定一个地址:可通过pingwww.baidu.com 获得。如果无需vim/etc/hosts删除百度相关一行,重新ping
server {
    listen 80;
    server_name www.baidu.com;

    location / {
      proxy_pass http://61.135.169.121/;
      #proxy_set_header Host $host;
    }
}
3.用 curl -x127.0.0.1:80 www.baidu.com 测试正常,代理访问完成

liuyu 发表于 2018-1-5 21:30:16

好牛逼啊学习了

strange 发表于 2018-1-6 21:45:46

很不错的总结

shixiaobo 发表于 2018-1-16 12:40:12

总结的很好

kang_12121 发表于 2018-4-9 14:55:43

666
页: [1]
查看完整版本: nginx 高级配置示例