zoukankan      html  css  js  c++  java
  • nginx跳转规则的记录

    1.首页跳转的示例:
    if ($host ~* ^chinasoft.com$){ rewrite ^(.*)$ https://www.chinasoft.com$1 permanent;}
    if ($request_uri ~ ^/(.*)/(index|indice).(html)) { rewrite ^/(.*)/(index|indice).(html) /$1   permanent;}
    if ($request_uri ~ ^/(index|indice).html) { rewrite    ^       / permanent;}

    2.跳转后去掉参数
    https://mobiletrans.chinasoft.com/buy/?f=pro  跳转到 https://mobiletrans.chinasoft.com/buy/a.html

    最终如下配置 跳转到了  https://mobiletrans.chinasoft.com/buy/a.html?f=pro

    if ($request_uri ~ ^/buy/?f=pro) { rewrite ^ https://mobiletrans.chinasoft.com/buy/a.html permanent; }
    if ($request_uri ~ ^/buy/$) { rewrite ^ https://mobiletrans.chinasoft.com/buy/a.html permanent; }

    # 加上?号,可以去掉 ? 后面的参数
    if ($request_uri ~ ^/buy/?f=pro) { rewrite ^ https://mobiletrans.chinasoft.com/buy/a.html? permanent; }


    3.多层目录下首页index.html跳转
    默认的跳转规则:
    [root@web02:/usr/local/nginx/conf/rewrite.d]# more film.chinasoft.com.conf
    if ($request_uri ~ ^/(.*)/(index|indice).(html)) { rewrite ^/(.*)/(index|indice).(html) /$1   permanent;}


    类似 index.html结尾的跳转,会跳转两次
    https://film.chinasoft.com/screen-recorder-faq/installation/index.html

    会跳到
    https://film.chinasoft.com/screen-recorder-faq/installation

    然后再次跳转到 https://film.chinasoft.com/screen-recorder-faq/installation/

    修改后
    # more film.chinasoft.com.conf
    if ($request_uri ~ ^/(.*)/(index|indice).(html)) { rewrite ^/(.*)/(index|indice).(html) /$1/   permanent;}


    https://film.chinasoft.com/screen-recorder-faq/installation/index.html 可以直接跳转到 https://film.chinasoft.com/screen-recorder-faq/installation/


    部分地区不行
    https://film.chinasoft.com/chinasoft-id-guide/index.html


    4.404页面的跳转问题

    a.配置了404页面,跳转不到根目录的404.html,即打开的错误页面不变,不发生301跳转就显示 /404.html
    error_page  404 403            /404.html;

    b.于是配置了 error_page  404 403            https://film.chinasoft.jp/404.html;  这样会导致大量的404发生301跳转,不利于seo优化

    c.之前配置nginx.conf的主配置添加下面参数就可以了,该次不生效

    proxy_intercept_errors on;
    fastcgi_intercept_errors on;

    d.想着模拟一次生产环境,进行验证
    ①.拷贝 生产的 /data/www/vhosts/film.chinasoft.jp 目录到yunweitools中
    ②.配置vhost.d和rewrite.d的配置,问题依旧说明不是nginx版本的问题
    ③.于是拷贝正常跳转的一个配置进行修改,问题解决
    ④.于是diff对比之前的配置和现在正常的配置,发现没有大的区别  httpsdocs 多了一个s,这才是问题的关键

     root            /data/www/vhosts/film.chinasoft.jp/httpdocs ; 是  root            /data/www/vhosts/film.chinasoft.jp/httpsdocs ;


    5.多个网站需要设置跨域的配置
    map $http_origin $corsHost {
        default 0;
        "~http://www.chinasoft.com" http://www.chinasoft.com;
        "~http://m.chinasoft.com" http://m.chinasoft.com;
    }

    https://shopcart.chinasoft.com,https://film.chinasoft.com'

    server
    {
        listen 80;
        server_name search.chinasoft.com;
        root /nginx;
        location /
        {
            add_header Access-Control-Allow-Origin $corsHost;
        }
    }


    6.404页面跳转不生效的问题处理

    [/usr/local/nginx/conf/vhost.d]# cat film.chinasoft.jp.conf
    server {
            listen 80;
            server_name     film.chinasoft.jp ori-film.chinasoft.jp ;
            access_log off;
            access_log      /data/www/logs/nginx_log/access/film.chinasoft.jp_access.log main ;
            error_log       /data/www/logs/nginx_log/error/film.chinasoft.jp_error.log ;
            root            /data/www/vhosts/film.chinasoft.jp/httpdocs ;
            index           index.html index.shtml index.php ;
            include                rewrite.d/film.chinasoft.jp.conf ;

            error_page  404 403            /404.html;

        rewrite ^/(.*)$ https://film.chinasoft.jp/$1 permanent;    #跳转到Https

    }

    server {
            listen 443;
            ssl on;

            ssl_certificate         cert2016/chinasoft_jp.crt;
            ssl_certificate_key     cert2016/chinasoft_jp.key;
            ssl_dhparam     cert2016/dh_2048.pem;

            ssl_session_timeout     30m;
            ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;


            ssl_ciphers     "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!AES128-GCM-SHA256:!AES256-GCM-SHA384:!AES128-SHA256:!AES256-SHA256:!AES128-SHA:!AES256-SHA:AES:!CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA";


            ssl_prefer_server_ciphers       on;

            add_header 'Access-Control-Allow-Origin' '*';

            server_name     film.chinasoft.jp ori-film.chinasoft.jp;
            access_log      /data/www/logs/nginx_log/access/film.chinasoft.jp_access.log main ;
            error_log       /data/www/logs/nginx_log/error/film.chinasoft.jp_error.log ;

            root            /data/www/vhosts/film.chinasoft.jp/httpdocs ;
            index           index.html index.shtml index.php ;
            include         rewrite.d/film.chinasoft.jp.conf ;

            error_page  404 403             /404.html;

            if ($http_user_agent ~ Ezooms) {
            return 403;
            }

            location ~ .php$ {
                            proxy_pass http://php_pool;
                            expires -1;
                            include proxy_params;
            }

            location / {
                    include proxy_params;
                    if (!-d $request_filename){
                            set $flag 1$flag;
                    }
                    if (!-f $request_filename){
                            set $flag 2$flag;
                    }
                    if ($flag = "21"){
                            proxy_pass http://php_pool;
                            expires -1;
                    }

            }

    }



    问题描述:访问 https://stu.chinasoft.com/aaaa/bbbb/ 类似这种不存在的页面返回首页,访问各种不存在的页面都返回首页,测试了很多种方法不行

    后面出了狠招,直接将环境移植到一台可以测试的服务器中(可以任意测试,没有风险)
    可能出现的问题:
    1.整体的nginx环境问题
    2.nginx配置问题
    3.页面有隐性跳转

    具体步骤:
    1.部署好nginx和php移植后台程序到测试服务器,本地绑定hosts,发现问题依旧,排除服务器环境问题
    2.将首页改为 <h1>index</h1> 404.html改为 <h1>404</h1>,避免页面内部问题,发现问题依旧,排除页面问题
    3.修改vhost/stu.chinasoft.com文件,将php部分注释掉发现问题没有了
    定位到是php部分的配置问题


    [root@EOP_chinasoft_web01:/usr/local/nginx/conf/vhost.d]# cat stu.chinasoft.com.conf
    server {
            listen 80;
            server_name     stu.chinasoft.com;
            access_log      /data/www/logs/nginx_log/access/stu.chinasoft.com_access.log main ;
            error_log       /data/www/logs/nginx_log/error/stu.chinasoft.com_error.log ;
            root            /data/www/vhosts/stu.chinasoft.com/httpdocs ;
            index           index.html index.shtml index.php ;
        include        rewrite.d/stu.chinasoft.com.conf ;
        error_page  404 403             /404.html;    

            rewrite ^/(.*)$ https://stu.chinasoft.com/$1 permanent;    #跳转到Https,http部分下面的代码可以忽略
            
            location ~ .php$ {
                    fastcgi_pass unix:/tmp/php-cgi.sock;
                    fastcgi_index index.php;
                    #fastcgi_param SCRIPT_FILENAME ;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                    expires -1;
            }

        location / {
                    include proxy_params;
                    if (!-d $request_filename){
                            set $flag 1$flag;
                    }
                    if (!-f $request_filename){
                            set $flag 2$flag;
                    }
                    if ($flag = "21"){
                                    rewrite ^(.*)$ /index.php last;
                            expires -1;
                    }
            }


    }

    server {
            listen 443;
            ssl on;

            ssl_certificate         cert2016/chinasoft_com.crt;
            ssl_certificate_key     cert2016/chinasoft_com.key;
            ssl_dhparam     cert2016/dh_2048.pem;

            ssl_session_timeout     5m;
            ssl_protocols   TLSv1 TLSv1.1 TLSv1.2;


            ssl_ciphers     "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-E
    CDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-S
    HA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:!AES128-GCM-SHA256:!AES256-GCM-SHA384:!AES128-SHA256:!AES256-SHA256:!AES128-SHA:!AES256-SHA:AES:!CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:ED
    H-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA";


            ssl_prefer_server_ciphers       on;
    #       add_header Strict-Transport-Security max-age=15768000;

            #ssl_stapling        on;
            #ssl_stapling_verify        on;


            server_name     stu.chinasoft.com;
            access_log      /data/www/logs/nginx_log/access/stu.chinasoft.com_access.log main ;
            error_log       /data/www/logs/nginx_log/error/stu.chinasoft.com_error.log ;

            root            /data/www/vhosts/stu.chinasoft.com/httpdocs ;
            index           index.html index.shtml index.php ;
            include         rewrite.d/stu.chinasoft.com.conf ;
            error_page  404 403             /404.html;

            location ~ .php$ {
                    fastcgi_pass unix:/tmp/php-cgi.sock;
                    fastcgi_index index.php;
                    #fastcgi_param SCRIPT_FILENAME ;
                    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                    include fastcgi_params;
                    expires -1;
            }
        
        # 问题就出在这里
            location / {
                    include proxy_params;
                    if (!-d $request_filename){
                            set $flag 1$flag;
                    }
                    if (!-f $request_filename){
                            set $flag 2$flag;
                    }
            # 问题就出在这里,直接注释掉即可
                    if ($flag = "21"){
                            rewrite ^(.*)$ /index.php last;
                            expires -1;
                    }
                error_page  404 403             /404.html;
            }

    }

    film.chinasoft.jp.conf配置
    # 404配置,访问不存在的页面不生效
    error_page  404 403              /404.html;

    # https://film.chinasoft.jp/404.html 页面存在
    于是修改
    error_page  404 403              https://film.chinasoft.jp/404.html;


    8.修改默认首页
    页面地址:
    https://recoverit.chinasoft.com/support/refund-and-renewal-faqs.html

    点击里面的链接会跳到

    http://support.chinasoft.com/&_ga=2.1314520.1688688#/article?id=1710

    返回1

    加入:
    location / {
              try_files $uri $uri/ /index.html;
        }


    需求:
    原URL地址                            跳至地址
    https://www.chinasoft.cc/conn/convert-3gp-to-flv.html    https://www.chinasoft.cc/

    访问https://www.chinasoft.cc/conn/convert-3gp-to-flv.html会自动跳转到主页:
    分析:
    1.可能是cdn缓存,发现这个站点没有用cdn排除
    2.跳转规则有问题,发现没有这样的跳转规则,这个站点除了默认的跳转规则没有做任何跳转
    3.将 /data/www/vhosts/httpds/conn/ 目录下的 convert-3gp-to-flv.html 文件备份,改成输出ok
    (本地指一台机器作为chinasoft.cc的机器)发现没有跳转,替换成原来的就跳转
    说明没有这个url的跳转规则,所以规则来自页面内的js或者html需要开发定位修改

    1.vhost.d规则限制了跳转
    原地址                            取消跳转至
    https://videoconn.chinasoft.com/fr/convert-mp4/    https://videoconn.chinasoft.com


    发现rewrite里面没有这个规则,是因为设置了

    error_page  404 403        https://videoconn.chinasoft.com ;

    当碰到404的时候跳转到首页

    2.当设置一个目录跳转时,目录下的子页面也全部跳转了

    # 跳转设置
    if ($request_uri ~ ^/download/) { rewrite ^ https://stu.chinasoft.com/downloads/ permanent; }
    if ($request_uri ~ ^/download/2.html) { rewrite ^ https://stu.chinasoft.com/downloads/ permanent; }
    if ($request_uri ~ ^/download/3.html) { rewrite ^ https://stu.chinasoft.com/downloads/ permanent; }

    发生了问题
    故障1
    访问 https://stu.chinasoft.com/download/download-high-school.html 跳转到了https://stu.chinasoft.com/downloads/

    分析:
    当设置了目录跳转 ^/download/ 开头的页面全部到 https://stu.chinasoft.com/downloads/会导致 download目录下的页面全部跳转到   https://stu.chinasoft.com/downloads/

    解决:添加限定符号$,只匹配一个页面
    if ($request_uri ~ ^/download/$) { rewrite ^ https://stu.chinasoft.com/downloads/ permanent; }

    故障2
    当访问 https://stu.chinasoft.com/download/ 出现404

    当用f12时发现访问  https://stu.chinasoft.com/download 出现403 forbidden
    解决办法
    chmod 755 /data/www/vhosts/stu.chinasoft.com/httpdocs/download

  • 相关阅读:
    AcWing 1027. 方格取数 dp
    AcWing 1014. 登山 dp
    acwing 482. 合唱队形 dp
    LeetCode 1463. 摘樱桃II dp
    LeetCode 100. 相同的树 树的遍历
    LeetCode 336. 回文对 哈希
    LeetCode 815. 公交路线 最短路 哈希
    算法问题实战策略 DARPA大挑战 二分
    算法问题实战策略 LUNCHBOX 贪心
    AcWing 1100. 抓住那头牛 BFS
  • 原文地址:https://www.cnblogs.com/reblue520/p/13749311.html
Copyright © 2011-2022 走看看