配置Nginx指向另外一台Nginx

配置Nginx指向另外一台Nginx

  • 背景
  • 方案
  • Nginx安装
  • Nginx配置
  • 注意事项

背景

公司的Restful接口使用白名单方式进行授权,当新的APP接入到系统时,需要将APP的IP地址添加到Rest服务的白名单中。
但是APP没有固定的IP地址,需要将每次更改后的IP地址进行收授权。

方案

1.再添加一台有固定IP的ECS,安装Nginx服务配置代理来解决该问题。
2.开通阿里云的负责均衡SLB服务,做IP映射,给客户暴露映射的地址。

经与客户沟通,选用方案1解决该问题。

Nginx安装

https://www.centos.bz/2018/01/centos-7%EF%BC%8C%E4%BD%BF%E7%94%A8yum%E5%AE%89%E8%A3%85nginx/

Nginx配置

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

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

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

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

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
			#这里指定需要指向的地址
		    proxy_pass http://api.*****.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;
        }
    }

}


注意事项

不能添加如下配置,因为需要让ECS所在服务器的IP作为访问restful接口的访问源。(如下配置的作用是使真正的请求源作为数据包头)
proxy_set_header Host $HOST;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

你可能感兴趣的:(Nginx,Nginx配置)