问题描述
例如有如下两个location配置location ~ /xxx/.+$ { proxy_pass http://tomcat; proxy_set_header Host $host; proxy_set_header X-URI $request_uri; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-Scheme $scheme; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }location ~ /yyy/.+$ { rewrite ^/yyy/(.+)$ /ddd?x=$1 break; proxy_pass http://tomcat; proxy_set_header Host $host; proxy_set_header X-URI $request_uri; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-Scheme $scheme; proxy_set_header X-Forwarded-Port $server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }两个location中有很多重复配置项,这些配置项有什么办法只配置一次就能在所有location中引用?
解决方案
1、在http 下加include proxy.confhttp { include mime.types; include proxy.conf;2、把代理相关的配置拿过去proxy.conf#!nginx (-) # proxy.confproxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 10m;client_body_buffer_size 128k;proxy_connect_timeout 90;proxy_send_timeout 90;proxy_read_timeout 90;proxy_buffers 32 4k;