nginx代理添加servlet路径

时间:2012-06-14 13:09:01

标签: tomcat nginx spring-security

我在路径“be”上运行Tomcat 6运行Web应用程序。 Spring安全性用于登录过程。登录网址为/console/login,登录后会重定向到/console/customer。我对login网址使用https,为customer网址使用http。我没有在任何弹簧配置文件中指定servlet路径。一切正常,并且在访问了/be/console/login正确的重定向(到/be/console/customer)后收到了。

现在我想要一个代理人。我将Nginx配置为将https://localhost转发到https://<someip>:9443,并将其配置为http,以便从端口80转发到端口9080.

问题是Nginx返回无效重定向到浏览器。而是/be/console/customer它会返回/be/be/console/customer。 Servlet路径被添加到URL两次。

如何删除第二个'be'?

感谢。

Nginx配置:

server {
    listen       443 ssl;
    server_name  localhost;

    ssl                  on;        
    ssl_certificate      C:\jc\\nginx\\tc\selfsigned.cer;
    ssl_certificate_key  C:\jc\\nginx\\tc\private.key;

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers   on;

    location / {
            proxy_redirect          off;
            proxy_pass              https://<some ip>:9443;
            proxy_set_header        X-Real_IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
    } 

    location ~ /.+ {
            proxy_redirect          off;
            proxy_pass              https://<some ip>:9443;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
    }   
}
server {
    listen       80;
    server_name localhost;

    access_log  logs/access.log;
    error_log  logs/error.log;

    location / {                
            proxy_redirect          off;
            #server_name_in_redirect    off;
            proxy_pass              http://<some ip>:9080;              
            proxy_set_header        X-Real_IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
    } 

    location ~ /.+ {
            proxy_redirect          off;                
            proxy_pass              http://<some ip>:9080;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
    }
}

1 个答案:

答案 0 :(得分:0)

此配置正在生产中:

location /demo/ {
    proxy_pass  http://localhost:8080;
    proxy_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;
    proxy_pass_request_headers on;

    client_max_body_size       10m;
    client_body_buffer_size    128k;

    proxy_connect_timeout      90;
    proxy_send_timeout         90;
    proxy_read_timeout         90;

    proxy_buffer_size          4k;
    proxy_buffers              4 32k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
}