nginx设置404页码不生效、指定域名跨域的设置

一、404页面配置后不能生效的问题

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

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

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

proxy_intercept_errors on;
fastcgi_intercept_errors on;

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


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

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

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

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

        rewrite ^/(.*)$ https://itube.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     itube.chinasoft.com;
        access_log      /data/www/logs/nginx_log/access/itube.chinasoft.com_access.log main ;
        error_log       /data/www/logs/nginx_log/error/itube.chinasoft.com_error.log ;

        root            /data/www/vhosts/itube.chinasoft.com/httpdocs ;
        index           index.html index.shtml index.php ;
        include         rewrite.d/itube.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;

页面地址:
https://recoverit.chinasoft.com/support/refund-and-renewal-faqs.html

点击里面的链接会跳到

http://support.chinasoft.com/&_ga=2.107563586.1749405195.1569403744-727936012.1569403744#/article?id=1710

返回1

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


三、nginx的配置指定域名可以跨域
map $http_origin $corsHost {
    default 0;
    "~http://www.123admin.com" http://www.123admin.com;
    "~http://m.123admin.com" http://m.123admin.com;
}

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

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

原文地址:https://www.cnblogs.com/reblue520/p/13157804.html