【发布时间】:2022-01-22 15:01:51
【问题描述】:
我正在尝试在使用 Dockerfile 找到一些匹配项后编写新行,但它不能按照我的代码工作。我在下面解释我的代码。
/etc/nginx/conf.d/ubot-nginx.conf::
server {
listen 90;
server_name ubot_nginx;
location / {
# proxy_pass http://flask_app:5005;
try_files $uri @app;
# Do not change this
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location @app {
include uwsgi_params;
uwsgi_pass unix:///tmp/uwsgi.sock;
}
location /site {
# rewrite ^/static(.*) /$1 break;
# root /static;
alias /app/static/angular;
}
}
在上面的文件中,我需要在alias /app/static/angular; 之后写一些新行i.e-try_files $uri $uri/ /index.html =404;,因此我在Dockerfile 中使用以下命令。
RUN sed '/^alias /app/static/angular.*/a try_files $uri $uri/ /index.html =404;' /etc/nginx/conf.d/ubot-nginx.conf
我需要在这里构建图像时,上面给出的新行应该根据找到的特定匹配来编写,这在我的情况下没有发生。请问有人可以帮我吗?
【问题讨论】:
-
如果你想用 sed 就地编辑文件,请使用
-i参数 stackoverflow.com/questions/15559359/… -
我不想在找到一些匹配项后替换添加新行。
-
不太确定我是否理解您想要的结果,但类似于
sed -i -E 's;^(\s*alias /app/static/angular.*);\1\n\ttry_files $uri $uri/ /index.html =404;' /etc/nginx/conf.d/ubot-nginx.conf? -
您对 sed 的调用只会将结果发送到标准输出,不会更改文件。如果要更改文件(称为就地编辑),则必须使用
-i参数。 -
@JoachimIsaksson,您的建议有效,但它在
try_files $uri $uri/ /index.html =404之后忽略了;,导致 nginx 出错。