在 nginx 上为应用程序添加上下文路径
Add context path for an app on nginx
Nginx服务器从根目录到根URL的所有静态内容。例如,如果根内容位置配置为包含文件 /usr/share/nginx/html/foo.html
的 /usr/share/nginx/html
,则 url http://localhost/foo.html
将为该 html 文件提供服务。我想在 URL 中添加一个上下文路径前缀,这样 http://localhost/myapp/foo.html
应该服务于 /usr/share/nginx/html/foo.html
文件。
我尝试更改位置并添加别名,但结果是 404。
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myapp/ {
alias /usr/share/nginx/html/;
}
我也希望 http://localhost/myapp
应该服务于 /usr/share/nginx/html/index.html
我正在使用 nginx 的 1.12.1-alpine
docker 图像
试试这个
index index.html index.htm;
location / {
root /usr/share/nginx/html;
}
location /myapp/ {
alias /usr/share/nginx/html/;
}
如果这不起作用,请尝试以下
index index.html index.htm;
location / {
root /usr/share/nginx/html;
}
location /myapp/ {
alias /usr/share/nginx/html/;
try_files $uri $uri/ =404;
}
编辑-1
如果你想让它在没有尾随 / 的情况下工作,那么你应该使用下面的
location ~ /app(/.*)?$ {
alias /usr/share/nginx/html/;
try_files $uri $uri/ =404;
}
Nginx服务器从根目录到根URL的所有静态内容。例如,如果根内容位置配置为包含文件 /usr/share/nginx/html/foo.html
的 /usr/share/nginx/html
,则 url http://localhost/foo.html
将为该 html 文件提供服务。我想在 URL 中添加一个上下文路径前缀,这样 http://localhost/myapp/foo.html
应该服务于 /usr/share/nginx/html/foo.html
文件。
我尝试更改位置并添加别名,但结果是 404。
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myapp/ {
alias /usr/share/nginx/html/;
}
我也希望 http://localhost/myapp
应该服务于 /usr/share/nginx/html/index.html
我正在使用 nginx 的 1.12.1-alpine
docker 图像
试试这个
index index.html index.htm;
location / {
root /usr/share/nginx/html;
}
location /myapp/ {
alias /usr/share/nginx/html/;
}
如果这不起作用,请尝试以下
index index.html index.htm;
location / {
root /usr/share/nginx/html;
}
location /myapp/ {
alias /usr/share/nginx/html/;
try_files $uri $uri/ =404;
}
编辑-1
如果你想让它在没有尾随 / 的情况下工作,那么你应该使用下面的
location ~ /app(/.*)?$ {
alias /usr/share/nginx/html/;
try_files $uri $uri/ =404;
}