我有一个运行Nginx的小型嵌入式Linux设备。我可以通过网络连接到它,并通过Chrome或Firefox访问PC上的端点。我的默认页面包含一个HTML标记,指向“loading.jpeg”,该标记位于设备/tmp/nginx/loading.jpeg上。我可以输入浏览器:http://192.168.0.4/loading.jpeg并查看我的图片。我也可以访问渲染html的端点,看看我的图像是否正确呈现。
现在我希望能够在浏览器中访问根页面http://192.168.0.4/并将其重定向到应该呈现html并显示图像的默认页面。问题是,如果我为默认的“/”位置设置页面,指向/ tmp / nginx的webserver根指令将不再起作用。所以我显示了我的页面,但找不到loading.jpeg图像。我已经尝试将根请求重定向到我的默认页面,但这也打破了Web服务器根目录。
如何为Nginx呈现默认网页,同时还要尊重我的网络服务器根?谢谢。
这不起作用(网络服务器root被破坏 - 虽然显示了预期的默认网页):
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;
}
这是我目前没有重定向的nginx.conf:
## 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)$" {
...
}
}
}
答案 0 :(得分:1)
有许多解决方案。使用location
的完全匹配rewrite ... last
块非常有效:
location = / {
rewrite ^ /some.html last;
}
有关详情,请参阅this document。