我有一个像这样的Apacheconfiguration:
<VirtualHost *:80> ProxyPass /app http://localhost:8080/app ProxyPassReverse /app http://localhost:8080/app ProxyPass /images http://localhost:8080/app/images ProxyPassReverse /images http://localhost:8080/app/images </VirtualHost>
我正试图把这个翻译成nginxconfiguration。 到目前为止,我有
server { listen 80; server_name localhost; location /app { proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080/app; } location /app/images { proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080/app/images; } ... }
这是很多重复。 请帮我弄清楚如何只有一个位置可以处理所有的子path。 此外,我想不启用任何(通配符)子path,但一组已知的。 说,我想启用localhost/app , localhost/app/images ,而不是其他人。 就像上面,但更短。 这可能吗?
nginx允许使用正则expression式匹配位置,假设你可能需要index.html 。 否则,只要删除它。
server { listen 80; server_name localhost; location ~ ^/app/(index.html|images)? { proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080; } ... }
你可以简单地使用这样的location块:
location /app { proxy_set_header X-Forwarded-Host $host:$server_port; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8080; }
当proxy_pass包含path组件时,nginx将把请求的规范化URI附加到proxy_pass指令的主机部分。