【问题标题】:Website pages are not opening correctly on nginx网站页面无法在 nginx 上正确打开
【发布时间】:2018-02-24 17:44:31
【问题描述】:

我正在尝试使用 Ubuntu 在 nginx 上设置 Question2Answer 网站 (yoalfaaz.com)。现在,网站的主页确实加载了,但任何其他页面都无法正确加载。大多数情况下,当我点击我网站上的任何帖子时,它会再次打开主页,有时会破坏布局。

这是sites-available file

server {
        listen 80 ;
        listen [::]:80 ;

        root /var/www/yoalfaaz.com/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name yoalfaaz.com www.yoalfaaz.com;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.php$is_args$args;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #

        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location ~ /\.ht {
        deny all;
        }
}

现在,以前只打开主页,对于其他每个页面,我都收到 404 Not Found 错误。所以我对try_files 行做了一些更改,之后网站页面没有以正确的方式打开。

我也检查了任何类型的错误,但没有任何错误,如果我尝试nginx -t,那么它也显示成功。请帮帮我,伙计们。

【问题讨论】:

    标签: nginx


    【解决方案1】:

    显然问题不在于 nginx,而在于您的应用程序。

    查看您网页的 HTML,我看到了:

    <link href="./qa-plugin/q2a-embed-master/qa-embed.css" rel="stylesheet">
    <link rel="stylesheet" href="./qa-plugin/q2a-tag-list-widget-master/tag-list.css?" TYPE="text/css">
    <link rel="stylesheet" href="./qa-plugin/Welcome-Widget-master/welcome-widget.css?" TYPE="text/css">
    

    您的 CSS 文件的 URL 是相对于当前路径的,因此如果 URL 包含类似于路径或子目录的内容,则基本上位置会发生变化。

    以这个网址为例:http://yoalfaaz.com/4966/pardesi-ke-naam

    尝试在该页面上加载 CSS 文件 ./qa-plugin/q2a-embed-master/qa-embed.css 将加载 http://yoalfaaz.com/4966/qa-plugin/q2a-embed-master/qa-embed.css,从而导致 404 错误。

    您应该更改代码以输出绝对 URL 或 root-relative URL。

    例子:

    • 绝对 URL:http://yoalfaaz.com/qa-plugin/q2a-embed-master/qa-embed.css//yoalfaaz.com/qa-plugin/q2a-embed-master/qa-embed.css(最后一个是 protocol-relative URL)
    • 相对于根的 URL:/qa-plugin/q2a-embed-master/qa-embed.css(总是从域的根开始)

    【讨论】:

    • 您好,感谢您抽出宝贵时间,我尝试了根相对 URL 方法,但没有成功。我仍然认为有一些 nginx 配置问题,因为在我以前的主机 (hostgator) 上,这个网站运行良好,我没有更改这个新主机上的网站文件中的任何内容。
    • 我再次访问了您的网站,但没有看到您对 CSS 文件使用相对于根目录的 URL。你真的可以尝试改变它,直到你在 HTML 源代码中获得那些根相对 URL 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-22
    相关资源
    最近更新 更多