TL;DR
我需要服务两个容器应用程序(每个在一个不同的端口)托管在同一台机器上,但位于两个非常不同的路径,使用一个nginx反向代理自己的静态子文件夹。
上下文
我经历了很多非常复杂的内容,这并不能帮助我理解如何使用nginx为每个位置提供一些静态文件夹的本质。
让我用一个简单而整洁的反向代理配置来解释我所说的“每个位置”是什么意思:
server {
listen 80;
listen [::]:80;
root /var/www;
server_name my.server.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-a {
proxy_pass http://127.0.0.1:8080/;
}
location /app-b {
proxy_pass http://127.0.0.1:8081/;
}
}这很好,除了静态文件(css,js,.)不提供服务。
因此,我对其进行了如下更改,以检索app-b的这些文件
server {
listen 80;
listen [::]:80;
root /var/www;
server_name my.server.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-a {
proxy_pass http://127.0.0.1:8080/;
}
location /app-b {
proxy_pass http://127.0.0.1:8081/;
}
location ^~ /static/ {
include /etc/nginx/mime.types;
root /home/debian/projects/crystallography/web/app-b/;
}
}这样做会更好;我现在有了这些用于app-b的静态文件。当然,对app-a来说还是什么都没有。
现在,你看我得到了什么.我还需要提供app-a自己的静态文件夹。
我测试过的
以下是我尝试过的两个主要想法(它们都不起作用)和其他变体(也不起作用):
server {
listen 80;
listen [::]:80;
root /var/www;
server_name my.server.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-a {
proxy_pass http://127.0.0.1:8080/;
}
location ^~ /static/ {
include /etc/nginx/mime.types;
root /media/biology/swamps/frog-migration/app-a/; # this is a totally different path than app-b
}
location /app-b {
proxy_pass http://127.0.0.1:8081/;
}
location ^~ /static/ {
include /etc/nginx/mime.types;
root /home/debian/projects/crystallography/web/app-b/;
}
}这显然与app-b中的静态文件夹冲突,因此nginx -t自然会失败:
nginx: [emerg] duplicate location "/static/" in /etc/nginx/sites-enabled/proxy-pass.conf:24
nginx: configuration file /etc/nginx/nginx.conf test failedserver {
listen 80;
listen [::]:80;
root /var/www;
server_name my.server.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-a {
proxy_pass http://127.0.0.1:8080/;
}
location ^~ /static/ {
include /etc/nginx/mime.types;
root /media/biology/swamps/frog-migration/app-a/;
}
}
server {
listen 80;
listen [::]:80;
root /var/www;
server_name my.server.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-b {
proxy_pass http://127.0.0.1:8081/;
}
location ^~ /static/ {
include /etc/nginx/mime.types;
root /home/debian/projects/crystallography/web/app-b/;
}
}语法是可以的,但是我对nginx -t有一些警告
nginx: [warn] conflicting server name "my.server.org" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "my.server.org" on [::]:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful但最重要的是,app-b不再是可访问的(返回404)。
问题
有了nginx,我如何才能让两个(或者更多的)不同的应用程序(位于同一台主机上的两条不同的路径)相互毗邻,在它们自己的气泡中生活,并与它们自己的自己的静态子文件夹(即两个应用的静态文件之间没有任何干扰)一起使用?
发布于 2021-02-05 16:56:35
我终于找到了这个解决方案,只有一个服务器正在工作,但我并不是百分之百满意,因为我想避免alias指令,因为路径遍历漏洞。
免责声明:所以请不要忘记别名位置中的尾随斜杠。
server {
listen 80;
listen [::]:80;
#root /var/www;
server_name server.domain.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-a {
proxy_pass http://127.0.0.1:8080/;
location /app-a/static/ {
include /etc/nginx/mime.types;
alias /media/biology/swamps/frog-migration/app-a/static/;
}
}
location /app-b {
proxy_pass http://127.0.0.1:8081/;
location /app-b/static/ {
include /etc/nginx/mime.types;
alias /home/debian/projects/crystallography/web/app-b/static/;
}
}
}特别是,它不使用root指令;因为它发送我的页面时没有任何静态文件(这些静态文件上的错误404,我不明白为什么):
server {
listen 80;
listen [::]:80;
#root /var/www;
server_name server.domain.org;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /app-a {
proxy_pass http://127.0.0.1:8080/;
location /app-a/static/ {
include /etc/nginx/mime.types;
root /media/biology/swamps/frog-migration/app-a/;
}
}
location /app-b {
proxy_pass http://127.0.0.1:8081/;
location /app-b/static/ {
include /etc/nginx/mime.types;
root /home/debian/projects/crystallography/web/app-b/;
}
}
}除了上面的工作解决方案之外,我还必须更改我的烧瓶应用程序中的静态路径,以便:
/app-a/static而不只是/static的app-a和
/app-b/static而不仅仅是应用程序b的/static。
但是,通过这样做,您肯定希望将app-a和app-b字符串放在环境变量中,而不是硬编码它们。
https://stackoverflow.com/questions/65972746
复制相似问题