开发者

Nginx rewrite problem

Every single page requested is passed to "check.php":

rewrite ^/(.*)$ http://localhost/check.php?page=$1

"check.php" verify several security things then require the requested page:

<?php
    // ... security stuff

    require_once ($myRequestedPage);
?>

The problem is, in an HTML file, any external style isn't loaded, just scripts:

<!doctype html>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- Do not load -->

        <link href="css.css" rel="stylesheet" type="text/css" /> <!-- Do not load -->

        <script s开发者_如何学Gorc="js.js"></script> <!-- Load -->

        <style>
            @import url("css.css"); <!-- Do not load -->
        </style>

        <title></title>
    </head>

    <body></body>
</html>

Any clues ? Thanks.


Do you currently care about manually setting the specific mime-type for every rewritten file via the PHP header command? The mime types differ for javascript, css and HTML files. This alone can lead to problems.


I recommend you ONLY route requests for files that don't exist to check.php. This prevents requests for "style.css" from being rewritten to check.php which is currently what is happening. EVERYTHING is being sent to check.php (even your stylesheets).

# Route all requests for non-existent files to index.php
if (!-e $request_filename) {
        rewrite ^/(.*)$ /check.php/$1 last;
}

# Pass PHP scripts to php-fastcgi listening on port 9000
location ~ \.php {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9000;
}


I suggest Firebug:

http://getfirebug.com/

That way, you can inspect what the browser is seeing and attempt to find what the problem is, which could be relating to paths (as Kyle mentions), or headers (are non text/css headers being sent due to the rewrite), or some other issue.

Checking what was downloaded, you can use the CSS tab and then look at each one of the files you were expecting to download. If your browser could not get the file, you will see an error. See the all.css portion of this screenshot:

Nginx rewrite problem

Then, you could inspect the Net tab and look at the headers/response for the css files, to see what was returned, as illustrated in the below screenshot (try headers to see if the Content-Type text/css is correct:

Nginx rewrite problem

Here is the header content-type highlighted:

Nginx rewrite problem


Try adding a / before the CSS link:

<link href="/css.css" rel="stylesheet" type="text/css" />

If that doesn't work, try:

<link href="../css.css" rel="stylesheet" type="text/css" />

Seems kind of trivial, but these things have given me similar problems before :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜