nginx虚拟主机常用配置

环境:centos-6.8,mysql-5.6,nginx-1.8,php-5.6,discuzX3.2

$ vim /usr/local/nginx/conf/vhosts/test.com.conf
server
{
    #监听端口
    listen 80;

    #服务器目录
    root /data/www;

    #访问日志文件位置和格式
    access_log /tmp/test.com_access.log combined_realip;

    #网站首页文件
    index index.html index.php;

    #域名
    server_name test.com www.test.com test2.com www.test2.com;

    #域名301重定向
    if ($host != 'www.test.com') {
        rewrite ^/(.*) http://www.test.com/$1 permanent;
    }

    #禁止指定的user_agent访问网站(主要针对垃圾UA爬虫)
    if ($http_user_agent ~* 'soso|yahoo|curl') {
        return 403;
    }

    #访问控制
    #禁止一个或几个IP访问
    deny 192.168.3.3;
    deny 192.168.3.6;
    #禁止IP段访问
    deny 192.0.0.0/8;
    deny 192.168.0.0/16;
    deny 192.168.3.0/24;
    #对某个文件进行访问控制
    location ^~ /auth/ {
        #允许192.168.3.9访问/auth/目录下的文件,其余拒绝。
        allow 192.168.3.9;
        deny all;
    }

    #用户认证
    #对站内以admin.php结尾的页面进行用户认证
    location ~ admin\.php$ {
        #基本用户认证
        auth_basic "authentication";
        #用户和密码文件,使用htpasswd生成
        auth_basic_user_file /usr/local/nginx/conf/vhosts/.htpasswd;
        #解析php文件
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #对目录/data/www/auth/下的文件访问需要用户认证
    location ^~ /auth/ {
        auth_basic "用户认证";
        auth_basic_user_file /usr/local/nginx/conf/vhosts/.htpasswd;
    }

    #解析php文件
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* \.(gif|jpg|jpeg|png|bmp|swf|zip|flv|mp3|mp4)$ {
        #对匹配文件不记录访问日志
        access_log off;
        #文件的客户端缓存过期时间
        expires 15d;
        #防盗链
        valid_referers none blocked *.test.com *.test2.com;
        if ($invalid_referer) {
            return 403;
            # rewrite ^/ http://www.xxx.com/.../xxx.gif;
        }
    }

    #设置js或css的URI的请求
    location ~* (js|css) {
        access_log off;
        expires 2h;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值