【问题标题】:nginx redirect from web root to a and if 404 then try bnginx 从 web 根重定向到 a,如果 404 则尝试 b
【发布时间】:2017-07-05 15:31:07
【问题描述】:

我正在尝试修改我的 nginx 配置以从 web 根 ("/") 重定向到 /dir-a,然后如果这返回 404 重定向到 /dir-b 。不过,我不完全确定这如何或是否可行。

这就是我目前所拥有的......

server {
    listen   443 ssl;
    ssl on;
    ssl_certificate /xyz.crt;
    ssl_certificate_key /xyz.key;

    root /var/www/mysite;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    client_max_body_size 2G;
    client_body_buffer_size 128k;

error_page 404 =200 /dir-b;
    location = / {
            try_files $uri $uri/ /dir-a;
    }

    # Serve static assets
    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {

            gzip  on;
            gzip_http_version 1.0;
            gzip_vary on;
            gzip_comp_level 9;
            gzip_proxied any;
            gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
            gzip_buffers 16 8k;

            access_log off;
            expires max;
            add_header Cache-Control public;

            break;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            include snippets/fastcgi-php.conf;

            fastcgi_buffer_size 256k;
            fastcgi_buffers 8 256k;
            fastcgi_busy_buffers_size 256k;

            fastcgi_param HTTPS on;
            fastcgi_read_timeout 6000;
            fastcgi_param APPLICATION_ENV production;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\.ht {
            deny all;
    }
}

但这会返回 404 页面,所以我不知道从哪里开始。我需要它来尝试 dir-b。

【问题讨论】:

    标签: nginx


    【解决方案1】:
    error_page 404 /dir-b;    
    location = / {
                    rewrite "/" /dir-a permanent;
            }
    

    但这将返回 404 代码,您可以更改它 error_page 404 =200 /dir-b;

    location = / {
                    rewrite "/" /dir-a permanent;
            }
    

    这将返回状态码 200

    您可以阅读this topic了解更多信息

    【讨论】:

    • 这仍然卡在 404 页面并且没有尝试 dir-b
    • 仍然卡在 404 for dir-a
    • 也许共享您的整个配置文件?确保没有其他东西覆盖你正在尝试做的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 2020-07-17
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多