Nginx配置可重用块

时间:2016-08-02 11:16:27

标签: nginx nginx-location

我将Nginx配置为某些应用程序服务器的反向代理,但是那里有很多重复的代码(proxy_pass行除外)。有没有什么方法可以将proxy_redirect和proxy_set_header行移动到某个公共块中,并为每个位置块引用它?

Nginx.conf:

location ^~ /users {
            proxy_pass http://192.168.0.1:5001;
            proxy_redirect http://api.example.com https://api.example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
}

location ^~ /users/verify {
            proxy_pass http://192.168.0.1:5002;
            proxy_redirect http://api.example.com https://api.example.com;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
}

1 个答案:

答案 0 :(得分:0)

不确定。您可以在配置的http块中执行此操作,如下所示:

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format    main  '$remote_addr - $remote_user [$time_local] "$request" '
                  '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

    #Rest of your config

    #Proxy settings
    proxy_redirect http://api.example.com https://api.example.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;

    include /etc/nginx/conf.d/*.conf;
}

请注意,这些设置将是" global"。