
Ich habe folgende Blockade.
location /api {
proxy_pass http://localhost:8080;
}
location /oauth2 {
proxy_pass http://localhost:8080;
}
location /login {
proxy_pass http://localhost:8080;
}
Ich möchte nach dem Proxy-Pass eine neue Zeile hinzufügen, aber nur im Abschnitt /API:
location /api {
proxy_pass http://localhost:8080;
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_send_timeout 300;
}
location /oauth2 {
proxy_pass http://localhost:8080;
}
location /login {
proxy_pass http://localhost:8080;
}
Es fällt mir schwer, den regulären Ausdruck richtig hinzubekommen.
Halbwegs erfolgreiche Versuche:
sed -re '/[[:space:]]proxy_pass http:\/\/localhost:8080;/a\ proxy_read_timeout 300;' nginx.con
^^^ Dies fügt offensichtlich read_timeout zu jeder Zeile hinzu, die die Proxy_pass-Direktive enthält, also habe ich versucht, Variationen von \r oder \n oder \r\n oder [[:space:]]+ am Anfang hinzuzufügen – ohne Erfolg.
Habe es andersherum probiert, mit etwas wie:
sed '/location \/api {\n\s+proxy_pass.*/a proxy_read_timeout 300' nginx.conf
aber auch dort scheitert
Kann mir jemand einen hilfreichen Tipp geben?
Antwort1
Herausforderung angenommen.
Versuchen Sie diesen Befehl:
sed 'N;/location \/api.*proxy_pass http:\/\/localhost:8080;/a \ proxy_read_timeout 300;\n proxy_connect_timeout 300;\n proxy_send_timeout 300;\n' nginx.conf
Die magischen Schlüsselwörter sind:
- das „N“ am Anfang Ihrer Sed-Anfrage, das Sed anweist, mehrere Zeilen abzugleichen.
- das „/a“, das sed anweist, nach Ihrer Übereinstimmung eine neue Zeile hinzuzufügen.
Und darüber hinaus fügen Sie passende Leerzeichen hinzu, die zu Ihrer Einrückung passen :)
EDIT: Zweiter Vorschlag.
sed 'N;/location \/api.*proxy.*$/a \ proxy_read_timeout 300;\n proxy_connect_timeout 300;\n proxy_send_timeout 300;\n' nginx