如何将请求重定向到Nginx到外部服务器?

时间:2016-08-10 12:49:35

标签: nginx http-proxy nginx-location

我已经阅读了文档,并看到了很多关于如何通过Nginx代理请求的示例,但我仍然无法做出简单的示例工作。
当我输入类似http://localhost:5050/maps/ ....的网址时,我希望Nginx使用该网址向Google地图发出请求。
这是我的配置文件:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       5050;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
            #access_log off;
            proxy_pass http://maps.googleapis.com;
            proxy_pass_request_body on;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }   
    }
}

但是当我向http://localhost:5050/maps/提出请求时,我得到了标准的404页面"未找到"。
error.log文件显示"的多个条目;系统找不到指定的文件" ,这很奇怪 - 我没有要求磁盘上的任何文件。
最重要的是,我抬头看着Fiddler - 没有对maps.googleapis.com的传出请求。
我在这做错了什么?示例网址为http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

1 个答案:

答案 0 :(得分:1)

我认为问题是 - 我没有正确地重新启动nginx(我在Windows上),所以当我关闭nginx.exe时,仍然有一个进程继续提供请求。
无论如何,这是一个适合我的版本:

worker_processes  1;

error_log logs/error.log;
pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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

    sendfile        on;

    keepalive_timeout  2000;

    server {
        listen       5050;
        server_name  localhost;

        location / {
            proxy_pass https://maps.googleapis.com;
            proxy_pass_request_body on;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }   
    }
}
相关问题