简体   繁体   English

Apache + NginX反向代理:在嵌套URL中提供静态文件和代理文件

[英]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. 我在端口8080上的服务器上运行了Apache,而在端口80上作为备用代理运行了NginX。我试图让NginX为特定的URL提供静态HTML文件。 I am struggling to write the NginX configuration that does this. 我正在努力编写实现此目的的NginX配置。 I have conflicting directives as my URLs are nested within each other. 我的URL相互嵌套,因此指令相互冲突。

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. 一个网址位于/example/ ,我希望NginX为服务器上位于/path/to/www/example-content.html的静态HTML文件提供服务,而不是让Apache将页面提供给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. 另一个URL位于/example/images/ ,我希望Apache将该页面提供给NginX,就像该站点的其余部分一样。

I have set up my nginx.conf file like this: 我已经像这样设置了我的nginx.conf文件:

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: 我尝试从NginX在/example/提供静态文件的过程如下:

    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. 此方法有效,但是它意味着/example/ URL之后的所有内容(例如/example/images/ )也都别名为该本地路径。

I would like to use regex instead but I've not found a way to serve up the file specifically, only the folder. 我想改用regex,但是我还没有找到一种方法来专门提供文件,仅提供文件夹。 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. 这特别匹配/example/文件夹,但是使用类似这样的文件名是无效的语法。 Using the explicit location = /example/ in any case doesn't work either. 在任何情况下,使用显式location = /example/都不起作用。

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). 既然您说您正在运行Apache,那么我假设它在那里可以运行动态内容,例如PHP(否则不需要)。 In that case, the example config below will serve all static content with Nginx and pass others to Apache. 在这种情况下,下面的示例配置将使用Nginx提供所有静态内容,并将其他内容传递给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". 这假定您正在遵循结构化的设置,其中每个文件夹中的默认文件称为“ index.html”或“ index.php”,例如“ /example/index.html”、“some-folder/index”。 html”和“ /some-other-folder/index.html”。

With this, navigating to "/example/", "/some-folder/" or "/some-other-folder/" will just work with no further action. 这样,导航至“ / example /”,“ / some-folder /”或“ / some-other-folder /”将不再需要任何操作。

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 如果每个文件夹都具有带有随机不同名称的默认文件,例如“ /example/example-content.html”、“some-folder/some-folder.html”和“ some-other-folder / yet-another-different-default” .html”,则变得更加困难,因为您需要执行以下操作

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. 更好的选择是在站点上具有结构化和逻辑上的文件布局,而不是多个不同的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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