PhilBot
PhilBot

Reputation: 60

Nginx default page and root webserver directive

I have a small embedded Linux device running Nginx. I can connect to it over the network and access the endpoints on a PC in Chrome or Firefox. My default page contains an HTML tag that points to "loading.jpeg", which is on the device at /tmp/nginx/loading.jpeg. I can type in the browser: http://192.168.0.4/loading.jpeg and see my image. I can also visit the endpoint that renders html and see my image rendered properly.

Now I want to be able to visit the root page: http://192.168.0.4/ in a browser and redirect that to my default page that should render the html and show the image. The problem is that if I set a page for the default "/" location, my webserver root directive pointing to /tmp/nginx no longer works. So I get my page displayed, but the loading.jpeg image is not found. I've tried redirecting the root request to my default page, but that also breaks the webserver root.

How can I render a default webpage for Nginx, while also having my webserver root honored? Thank you.

This does not work ( webserver root is broken - though expected default webpage is shown ):

location / {

            default_type text/html;
            content_by_lua_file /sbin/http/serve_stream.lua;

            ## The streaming endpoint
            location /streaming {

                default_type text/html;
                content_by_lua_file /sbin/http/serve_stream.lua;

            }

Here is my current nginx.conf without a redirect:

## Setup server to handle URI requests
server {

    # Setup the root
    root /tmp/nginx;

    ## Port
    listen       80; ## Default HTTP

    ## Android phones from Ice Cream Sandwich will try and get a response from
    server_name
            clients3.google.com
            clients.l.google.com
            connectivitycheck.android.com
            apple.com
            captive.apple.com;

    ## We want to allow POSTing URI's with filenames with extensions in them
    ## and nginx does not have a "NOT MATCH" location rule - so we catch all
    ## and then selectively disable ones we don't want and proxy pass the rest
    location / {

        # For Android - Captive Portal
        location /generate_204 {
            return 204;
        }

        # For iOS - CaptivePortal
        if ($http_user_agent ~* (CaptiveNetworkSupport) ) {
            return 200;
        }

        ## Raw WebSocket
        location /ws {

            lua_socket_log_errors off;
            lua_check_client_abort on;

            default_type text/html;
                content_by_lua_file /sbin/http/websocket.lua;

        }

        ## The streaming endpoint
        location /streaming {

            default_type text/html;
            content_by_lua_file /sbin/http/serve_stream.lua;

        }

        ## We can have file extensions in POSTing of /blahendpoints for filesystem
        ## control HTTP commands
        location ~ "\.(txt|bin)$" {

            ...

        }

    }

}

Upvotes: 1

Views: 608

Answers (1)

Richard Smith
Richard Smith

Reputation: 49812

There are a number of solutions. An exact match location block with a rewrite ... last is quite efficient:

location = / {
    rewrite ^ /some.html last;
}

See this document for more.

Upvotes: 1

Related Questions