2016-11-22 127 views
0

我的改写作品。NGINX - 重写和错误404

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

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

同样,当我把我的链接管理员/它把我的开幕式,菜单和copiryght。

举例如果我把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; 
    } 

} 

+0

尝试将'root /var/www/my.domain.com/;'移动到服务器块内的位置/ {...}块中。 – AaronDanielson

+0

我只是尝试,但它使我工作的网页无处不在的错误 –

回答

0

这里的问题是,你重写告诉nginx一切重定向到后端,所以nginx的不检测404本身。您的后端然后处理请求并发送一个404,nginx直接发送回客户端。

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

+0

感谢所有的帮助,我已经解决了这个问题。错误出现在块的位置的第三个正则表达式中,'(。*)'替换了其所有的字符。我已经用'([\ w - ] +)替换了我的表达式,这是工作。 –