简体   繁体   中英

Apache + NginX reverse proxy: serving static and proxy files within nested URLs

I have Apache running on my server on port 8080 with NginX running as a reserve proxy on port 80. I am attempting to get NginX to serve static HTML files for specific URLs. I am struggling to write the NginX configuration that does this. I have conflicting directives as my URLs are nested within each other.

Here's what I want to have:

  • One URL is at /example/ and I want NginX to serve a static HTML file located on my server at /path/to/www/example-content.html instead of letting Apache serve the page to NginX.

  • Another URL is at /example/images/ and I want Apache to serve that page to NginX, just as it does for the rest of the site.

I have set up my nginx.conf file like this:

server {
    listen          80;
    server_name     localhost;
    # etc...

    location / {
        proxy_pass  http://127.0.0.1:8080/;
    }

    # etc...    

My attempt to serve the static file at /example/ from NginX went like this:

    location /example/ {
        alias  /path/to/www/;
        index  example-content.html;
    }

This works, but it means everything after the /example/ URL (such as /example/images/ ) is aliased to that local path also.

I would like to use regex instead but I've not found a way to serve up the file specifically, only the folder. What I want to be able to say is something like this:

    location ~ ^/example/$ {
        alias  /path/to/www/example-content.html;
    }

This specifically matches the /example/ folder, but using a filename like that is invalid syntax. Using the explicit location = /example/ in any case doesn't work either.

If I write this:

    location ~ ^/example/$ {
        alias  /path/to/www/;
        index  example-content.html;
    }

    location ~ /example/(.+) {
        alias  /path/to/www/$1;
    }

The second directive attempts to undo the damage of the first directive, but it ends up overriding the first directive and it fails to serve up the static file.

So I'm at a bit of a loss. Any advice at all extremely welcome! Many thanks.

Since you say you have Apache running, I assume it is there to run dynamic content such as PHP (Otherwise it is not needed). In that case, the example config below will serve all static content with Nginx and pass others to Apache.

server {
    # default index should be defined once as high up the tree as possible
    # only override lower down if absolutely required
    index index.html index.php

    # default root should be defined once as high up the tree as possible
    # only override lower down if absolutely required
    root /path/to/www/

    location / {
        try_files $uri $uri/ @proxy;
    }
    location @proxy {
        proxy_pass  http://127.0.0.1:8080;
        # Other Proxy Params 
    }
    location ~ \.php$ {
        error_page 418 = @proxy
        location ~ \..*/.*\.php$ { return 400; }
        return 418;
    }
}

What this assumes is that you are following a structured setup where the default file in every folder is either called "index.html" or "index.php" such as "/example/index.html", "some-folder/index.html" and "/some-other-folder/index.html".

With this, navigating to "/example/", "/some-folder/" or "/some-other-folder/" will just work with no further action.

If each folder has default files with random different names, such as "/example/example-content.html", "some-folder/some-folder.html" and "some-other-folder/yet-another-different-default.html", then it becomes a bit more difficult as you then need to do something like

server {
    # default index should be defined once as high up the tree as possible
    # only override lower down if absolutely required
    index index.html index.php

    # default root should be defined once as high up the tree as possible
    # only override lower down if absolutely required
    root /path/to/www/

    location / {
        try_files $uri $uri/ @proxy;
    }
    location @proxy {
        # Proxy params 
        proxy_pass  http://127.0.0.1:8080;
    }
    location ~ .+\.php$ {
        error_page 418 = @proxy
        location ~ \..*/.*\.php$ { return 400; }
        return 418;
    }
    location /example/ {
        # Need to keep defining new index due to lack of structure
        # No need for alias or new root
        index  example-content.html;
    }
    location /some-folder/ {
        # Need to keep defining new index due to lack of structure
        # No need for alias or new root
        index  some-folder.html;
    }
    location /some-other-folder/ {
        # Need to keep defining new index due to lack of structure
        # No need for alias or new root
        index  yet-another-different-default.html;
    }
    # Keep adding new location blocks for each folder
    # Obviously not the most efficient arrangement
}

The better option is to have a structured and logical layout of files on the site instead of multiple differing locations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM