天乐博客
扫描关注天乐博客

手机扫描二维码

www.mydomain.com Nginx配置备份

天乐博客2018-08-01未分类 527
Nginx不熟悉,折腾了一阵子搞成这样
1.移动端访问指定到不同目录从而实现模板自适应
2.禁止一些目录运行php文件
3.织梦伪静态的改造
4.s.mydomain.com为静态资源访问域名
server
{
    listen 80;
    listen 443 ssl;
    server_name www.mydomain.com m.mydomain.com s.mydomain.com mydomain.com xxxxxx.mydomain.com;
    index index.html index.php index.htm;

    set $rt "";
    set $is_mobile "is_false";
    if ( $http_user_agent ~* "(Android|webOS|iPhone|iPad|Windows Phone|iPod|BlackBerry|SymbianOS|Nokia|Mobile)" ){
        set $rt "/m";
        set $is_mobile "is_true";
    }
    if ( $host = 's.mydomain.com' ){
        rewrite .*\.(php|ini|inc) http://www.mydomain.com/ redirect;
        set $rt "";
        set $is_mobile "is_false";
    }
    root /www/wwwroot/www.mydomain.com$rt;
    
    location ~ /(templets|uploads|a|ad|images|static|data|special)/(.*)\.php {
        return 403;
    }
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    ssl_certificate    /etc/letsencrypt/live/www.mydomain.com/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/www.mydomain.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;
    error_page 497  https://$host$request_uri;

    #SSL-END
    if ( $host = 'mydomain.com' ){
        rewrite ^/(.*)$ http://www.mydomain.com/$1 permanent;
    }
    if ( $host = 'm.mydomain.com' ){
        rewrite ^/(.*)$  http://www.mydomain.com/$1 permanent;
    }
    if ( $host = 'xxxxxx.mydomain.com' ){
        rewrite ^/index\.(php|html)$ http://www.mydomain.com/index.php redirect;
        rewrite ^/(.*)/([0-9]+)\.html$ http://www.mydomain.com/$1/$2.html redirect;
        rewrite ^/(.*)/$ http://www.mydomain.com/$1/ redirect;
        rewrite ^/(.*)/list_([0-9]+)_([0-9]+)\.html$ http://www.mydomain.com/$1/list_$2_$3.html redirect;
    }
    if ( $host = 'www.mydomain.com' ){
        rewrite /(?!.*(index|search|ad_js|list|view|mobile|qrcode|ajax_tuku|tags))(.*)\.php http://www.mydomain.com/ redirect;
        rewrite ^/de_xxxxxx_de/(.*)$ http://www.mydomain.com/ redirect;
    }
    if ($server_port !~ 80){
        rewrite ^(/.*)$ http://$host$1 permanent;
    }
    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    error_page 404 /404.html;
    error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    include enable-php-53.conf;
    #PHP-INFO-END

    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/www.mydomain.com.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires      30d;
        error_log off;
        access_log off;
    }
    
    location ~ .*\.(js|css)?$
    {
        expires      240h;
        error_log off;
        access_log off; 
    }
    access_log  off;
    error_log  /www/wwwlogs/www.mydomain.com.error.log;
}



伪静态规则如下:
if (!-e $request_filename){
    rewrite ^/m/(.*)$ http://www.mydomain.com/$1 permanent;
    set $is_mobile "${is_mobile}m";
}

if ( $is_mobile = "is_falsem" ){
rewrite ^/(.*)/([0-9]+)\.html$ /plus/view.php?aid=$1 break;
rewrite ^/(.*)/([0-9]+)_([0-9]+)\.html$ /plus/view.php?aid=$1&pageno=$2 break;
rewrite ^/(.*)/$ /plus/list.php?tid=$1 break;
rewrite ^/(.*)/list_([0-9]+)_([0-9]+)\.html$ /plus/list.php?tid=$1&PageNo=$2 break;
rewrite ^/tags\.html$ /tags.php break;
rewrite ^/tags/(.*)(?:(\?.*))* /tags.php\?\/$1 break;
rewrite ^/tags/(.*)\/(?:(\?.*))*  /tags.php\?\/$1\/ break;
rewrite ^/tags/(.*)\/([0-9])(?:(\?.*))* /tags.php\?\/$1\/$2 break;
rewrite ^/tags/(.*)\/([0-9])\/(?:(\?.*))*  /tags.php\?\/$1\/$2\/ break;
rewrite ^/search/(.*)-([0-9]+)\.html$  /plus/search.php?q=$1&PageNo=$2&pagesize=10&searchtype=title break;
rewrite ^/search/(.*)\.html$  /plus/search.php?q=$1&pagesize=10&searchtype=title break;
}
if ( $is_mobile = "is_truem" ){
rewrite ^/index\.(html|php)$ /index.php break;
rewrite ^/(.*)/([0-9]+)\.html$ /view.php?aid=$1 break;
rewrite ^/(.*)/([0-9]+)_([0-9]+)\.html$ /view.php?aid=$1&pageno=$2 break;
rewrite ^/(.*)/$ /list.php?tid=$1 break;
rewrite ^/(.*)/list_([0-9]+)_([0-9]+)\.html$ /list.php?tid=$1&PageNo=$2 break;
rewrite ^/search/(.*)-([0-9]+)\.html$  /search.php?q=$1&PageNo=$2&pagesize=10&searchtype=title break;
rewrite ^/search/(.*)\.html$  /search.php?q=$1&pagesize=10&searchtype=title break;
rewrite ^/tags\.html$ /tags.php break;
rewrite ^/tags/(.*)(?:(\?.*))* /tags.php\?\/$1 break;
rewrite ^/tags/(.*)\/(?:(\?.*))*  /tags.php\?\/$1\/ break;
rewrite ^/tags/(.*)\/([0-9])(?:(\?.*))* /tags.php\?\/$1\/$2 break;
rewrite ^/tags/(.*)\/([0-9])\/(?:(\?.*))*  /tags.php\?\/$1\/$2\/ break;
}


发表评论