【问题标题】:NGINX - rewrite and error 404NGINX - 重写和错误 404
【发布时间】:2017-04-06 16:05:48
【问题描述】:

我的重写工作。

另一方面,我的 404 错误重定向不再起作用。

如果我添加到我的 .php 链接,它会返回我没有文件。

同样,当我在我的链接中添加一个管理员/它给我打开、菜单和版权。

例如,如果我输入https://mon.domain.com/admin/,它会加载我一个页面,而我想要我的 404 错误页面。

我想要什么,如果页面不存在,无论如何我都会遇到错误 404

你能帮帮我吗?

这里是nginx的配置:

upstream www {
    server unix:/var/run/php5-fpm.sock;
}

server {
    listen 80 default;
    server_name no-impact.eu;
        return 301 https://my.domain.com;
}
server {
    listen 443 ssl;
    server_name my.domain.com;

    fastcgi_param HTTPS on;
    ssl on;
        ssl_certificate /etc/letsencrypt/live/my.domain.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/my.domain.com/privkey.pem;
        ssl_session_timeout  5m;
        ssl_protocols  SSLv2 SSLv3 TLSv1;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers   on;

    root /var/www/my.domain.com/;
    index index.php;

    error_page 400 401 402 403 404 500 502 503 504 /error.html;

    location /.well-known/acme-challenge {
        root /var/www/letsencrypt;
    }

    location = /error.html {
        root  /var/www/my.domain.com/error/;
        index index.html;
    }

    location / {
        index index.php index.html;
        try_files $uri $uri/ @rewrite;
    }

    location @rewrite {
        rewrite ^/([\w-]+)-page-(\d+)$ /index.php?id=$1&page=$2 last;
        rewrite ^/([\w-]+)-(\d+)$ /index.php?id=$1&id_tutoriel=$2 last;
        rewrite ^/(.*)$ /index.php?id=$1 last;
    }


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

【问题讨论】:

  • 尝试将root /var/www/my.domain.com/; 移动到服务器块内的location / {...} 块中。
  • 我只是尝试,但它让我在有效的页面上到处都是错误

标签: nginx configuration url-rewriting nginx-location


【解决方案1】:

这里的问题是您的重写告诉 nginx 将所有内容重定向到后端,因此 nginx 本身不会检测到 404。然后您的后端处理请求并发送 404,nginx 将其直接发送回客户端。

为了让 nginx 拦截 404 并显示自己的错误页面,您需要将指令 fastcgi_intercept_errors on; 添加到您的 .php-location。

【讨论】:

  • 感谢大家的帮助,我已经解决了这个问题。错误出现在块 location / {} 的第三个正则表达式中,(.*) 替换了这个 / 的所有字符。我已经用([\w-]+) ant 替换了我的表达方式。
猜你喜欢
  • 1970-01-01
  • 2014-05-12
  • 2017-09-24
  • 1970-01-01
  • 2014-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多