zoukankan      html  css  js  c++  java
  • Nginx虚拟主机配置模板

    /////////////////////////////写在前头//////////////////////////////////////////
    Nginx 服务器中文文档:
    http://www.nginx.cn/doc

    Nginx 变量:
    http://blog.sina.com.cn/s/blog_594c47d00101dfyh.html

    nginx.conf 配置详解:
    http://www.ha97.com/5194.html

    nginx rewrite 参数和例子
    http://blog.c1gstudio.com/archives/434

    /////////////////////////////////目录//////////////////////////////////////////
    1.用户认证
    2.静态文件不记录日志
    3.伪静态
    4.防盗链
    5.日志
    6.访问控制
    7.代理


    /////////////////////////////////////////////////////////////////////////////////
    用户认证:
    准备工作:
    yum install httpd
    htpasswd -c /usr/local/nginx/conf/.htpasswd username
    if it the first time to use the tool htpasswd , please use -c

    配置参考如下:
    server {
    listen 80;
    server_name yourdomain.com;
    index index.html index.htm index.php;
    root /yourpath;
    location ~ .*admin.php$ {
    auth_basic "aminglinux auth";
    auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
    #真正起作用的是上面两句,实质上是通过密钥认证
    ...
    }
    }


    /////////////////////////////////////////////////////////////////////////////////
    静态文件不记录日志:
    配置参考如下:
    location ~ ^.*.(gif|jpg|jpeg|png|bmp|swf|zip|pdf|bz2|flv|doc|xls|gz|rar)$ {
    expires 10d;
    access_log off;
    }
    location ~ .*.(js|css)?$ {
    expires 12h;
    access_log off;
    }


    /////////////////////////////////////////////////////////////////////////////////
    伪静态:
    模板如下:
    rewrite ^([^.*])/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;
    rewrite ^([^.*])/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
    rewrite ^([^.*])/thread-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
    rewrite ^([^.*])/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2page=$3 last;
    rewrite ^([^.*])/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;

    Nginx的Rewrite设置及示例
    http://ask.apelearn.com/question/239

    nginx $document_uri 参数使用
    http://ask.apelearn.com/question/993

    nginx的301与302如何配置
    http://www.lishiming.net/thread-4840-1-1.html

    /////////////////////////////////////////////////////////////////////////////////
    防盗链:
    配置参考如下:
    location ~* ^.+.(gif|jpg|jpeg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpg|bmp|xls)${
    valid_referers none blocked server_names *.taobao.com *.baidu.com *.google.com *.google.cn *.soso.com;
    if($invalid_referer){
    return 403;
    }
    }


    /////////////////////////////////////////////////////////////////////////////////
    配置访问日志:
    编写日志切割脚本:
    vi /usr/local/sbin/logrotate.sh //加入
    #!/bin/bash
    datedir=`date -d "-1 day"+%Y%m%d`
    /bin/mkdir /home/logs/$datadir > /dev/null 2>&1
    /bin/mv /home/logs/discuz.log /home/logs/discuz_$datedir.log
    /etc/init.d/nginx reload >/dev/null 2> /dev/null
    #reload之后就生成了一个新的discuz.log


    server{
    access_log /home/logs/discuz.log combined_realip
    }

    日志格式

    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 main1 '$proxy_add_x_forwarded_for -$remote_user[$time_local]'
    '"$request" $status $body_bytes_sent'
    '"$http_referer" "$http_user_agent"'; //此日志格式为,ip不仅记录代理的ip,还记录远程客户端真实ip


    错误日志error_log日志级别

    error_log 级别分为 debug, info, notice, warn, error, crit 默认为crit,该级别在日志名后面定义格式如下
    error_log /your/path/error.log crit;
    crit记录的日志最少,而debug记录的日志最多,如果你的nginx遇到一些问题,比如502比较频繁出现,但是看默认的error_log没有看到有意义的信息,那么就可以调一下错误日志级别,当你调成error级别时,错误日志记录的内容会更加丰富

    /////////////////////////////////////////////////////////////////////////////////
    配置访问控制:
    根据目录来限制php解析:
    location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*.php${
    deny all;
    #这个类似Apache的php_engine off
    }

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


    /////////////////////////////////////////////////////////////////////////////////
    配置Proxy:
    server {
    listen 80;
    server_name www.w1.discuz.com;
    index index.html index.htm index.php;
    root /data/www;
    location ~ .php$ {
    proxy_pass http://192.168.75.132:8080;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }

    【拓展】
    根据访问的目录来区分后端的web
    http://www.lishiming.net/thread-920-1-1.html
    针对请求的uri来代理
    http://www.lishiming.net/thread-1049-1-1.html

  • 相关阅读:
    从原生web组件到框架组件源码(二)
    从原生web组件到框架组件源码(一)
    拖拽滚动视图(一)
    SVG研究之路(一)下
    运算符
    编码
    格式化输出
    循环语句
    条件语句
    Python基础
  • 原文地址:https://www.cnblogs.com/ImJerryChan/p/6202643.html
Copyright © 2011-2022 走看看