【问题标题】:How to change the root path in Nginx configuration如何更改 Nginx 配置中的根路径
【发布时间】:2021-12-08 12:50:52
【问题描述】:

我创建了一个简单的 HTML 应用程序,并尝试使用 nginx 服务器托管它。

index.html

<!DOCTYPE html>

<html lang="en">

<head>
<title>Simple Test Web App!</title>
</head>

<body >
Test Web App!!!
</body>

</html>

Dockerfile

FROM nginx:latest

# Copy files and directories from the application
COPY index.html /usr/share/nginx/html

# Tell Docker we are going to use this port
EXPOSE 8080

Docker 构建 docker build -t webapp .

Docker 运行 docker run -p 8080:8080 webapp:latest

我可以使用 http://localhost:8080/ URL 运行应用程序。但我想知道我们如何将根路径更改为 http://localhost:8080/webapp URL。这意味着它应该为来自 http://localhost:8080/webapp URI 的请求提供服务正在使用 http://localhost:8080/

【问题讨论】:

    标签: docker nginx nginx-reverse-proxy nginx-config


    【解决方案1】:

    如果您想使用 HTTP://localhost:8080/webapp 等子目录而不是 HTTP://localhost:8080 来重定向 HTML 页面, 为此,您需要创建 nginx 配置文件,将您的 HTML 页面重定向到您的子目录 URL。在您的 nginx 配置文件中,您应该添加以下行,

    location /htmlsite { root /var/www/htmlsite; try_files $uri $uri/ =404; }

    【讨论】:

    • 感谢您的回复。我已经添加了您建议的更改,但它给出了 404 错误。同时点击localhost:8080/webapp
    猜你喜欢
    • 2014-09-26
    • 2018-05-29
    • 2018-07-22
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2015-11-14
    相关资源
    最近更新 更多