java - 在 nginx 代理后面运行的 tomcat webapp 中的远程 IP

标签 java tomcat nginx proxy

使用 nginx/1.10.2 代理和 tomcat7 我试图通过 request.getRemoteAddr()< 获取 java webapp 中的实际远程 ip 地址 但总是得到 127.0.0.1。这是我所做的:

CentOS 代理通行证:setsebool -P httpd_can_network_connect true
& 除了 http/ajp 端口更改外,tomcat7 上没有配置。

Nginx 配置:

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  off;

    client_body_in_file_only clean;
    client_body_buffer_size 32K;

    client_max_body_size 50M;

    sendfile            on;
    send_timeout        300s;

    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         /var/www/html;
        index index.php index.html index.htm;

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

        location / {
            try_files $uri $uri/ =404;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
}



Nginx 代理配置:

upstream my_tomcat {
        server 127.0.0.1:81;
}
server {
        listen  80;
        server_name     sub.domain.com;
        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                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_set_header X-NginX-Proxy true;
                proxy_pass http://my_tomcat;
                proxy_redirect off;

                proxy_connect_timeout       300;
                proxy_send_timeout          300;
                proxy_read_timeout          300;
                send_timeout                300;
        }
}
server {
        listen  80;
        server_name     www.sub.domain.com;
        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_http_version 1.1;
                proxy_set_header Connection "";
                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_set_header X-NginX-Proxy true;
                proxy_pass http://my_tomcat;
                proxy_redirect off;

                proxy_connect_timeout       300;
                proxy_send_timeout          300;
                proxy_read_timeout          300;
                send_timeout                300;
        }
}

最佳答案

您应该使用 HTTP header X-Real-IP 来获取真实的远程 ip。

“如何”写在下面

request.getHeader("X-Real-IP")

“为什么”是,作为代理过程的一部分,代理在 http header X-Real-IP 中添加了真实的客户端 IP。

关于java - 在 nginx 代理后面运行的 tomcat webapp 中的远程 IP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42661710/

相关文章:

nginx - 重用 nginx.conf 中域的配置语句

java - java子类是否使用相同的父类(super class)字段对象

ruby-on-rails - 在 Rails 开发中使用 solr 和 sunspot

tomcat - Grails 应用程序无法在 Tomcat7 中启动 - 无法从日志中获取任何有用的信息?

mysql - 为 EC2 上的高流量调整 php-fpm、nginx、mysql

php - 我的网站随机变慢并且图表显示没有负载

java - 关于 ServletConfig 实例化的简单 Servlet 问题

java - 过滤负值

java - 无法在 WebSphere 中以编程方式创建 JMS 主题

java - NamingContext 无法转换为数据源?