nginx:配置 proxy_pass 时出现问题

nginx:配置 proxy_pass 时出现问题

我正在将 Web 应用程序从 apache 转换为 nginx。在 apache 的 httpd.conf 中,我有:

ProxyPass /proxy/ http://
ProxyPassReverse /proxy/ http://

这个想法是客户端发送这个 url:

http://web-server-domain/proxy/login-server-addr/loginUrl.php?user=xxx&pass=yyy

Web 服务器调用:

http://login-server-addr/loginUrl.php?user=xxx&pass=yyy

我的 nginx.conf 附在下面,但它无法正常工作。目前它似乎正在调用服务器,但返回应用程序错误。这看起来很有希望,但任何调试它的尝试都失败了!我无法跟踪任何调用,因为 nginx 拒绝将它们放在错误文件中。此外,在登录服务器上放置 echo 语句也无济于事,这很奇怪。

nginx 文档对此没有什么帮助。

关于如何配置 proxy_pass 有什么建议吗?

谢谢!

user              nginx;
worker_processes  1;

#error_log  /var/log/nginx/error.log;
error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}


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"';

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

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

#
# The default server
#
server {
     rewrite_log on;

    listen       80;
    server_name  _;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

#root /var/www/live/html;
index  index.php index.html index.htm;

   location ~ ^/proxy/(.*$) {
#location /proxy/ {
#   rewrite ^/proxy(.*) http://$1 break;
    proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_buffering off;
          proxy_pass http://$1;
    #proxy_pass "http://173.231.134.36/messages_2.7.0/loginUser.php?userID=ofer.fly%40gmail.com&password=y4HTD93vrshMNcy2Qr5ka7ia0xcaa389f4885f59c9";
    break;
   }

   location / {
    root /var/www/live/html;

    #if ( $uri ~ ^/proxy/(.*) ) {
        #   proxy_pass http://$1;
    #   break;
        #}

    #try_files  $uri $uri/ /index.php;
   }

    error_page  404              /404.html;
    #location = /404.html {
    #    root   /usr/share/nginx/html;
    #}

    # redirect server error pages to the static page /50x.html
    #
    #error_page   500 502 503 504  /50x.html;
    #location = /50x.html {
    #    root   /usr/share/nginx/html;
    #}

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        #fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name; 
        fastcgi_param  SCRIPT_FILENAME  /var/www/live/html$fastcgi_script_name; 
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;

}

答案1

好的,我在我的 nginx 测试平台上尝试了这个,并在日志中得到以下内容:

2011/08/29 22:35:31 [error] 10074#0: *1257 no host in upstream "", client: 10.103.0.9, server: localhost, request: "GET /proxy/ya.ru HTTP/1.1", host: "10.110.0.42"

因此,nginx 并不是一个合适的工具,因为无法在运行时定义上游主机。

相关内容