相关疑难解决方法(0)

Docker Swarm在Nginx中获得真正的IP(客户端主机)

我有一个使用nginx和PHP的堆栈在Docker Swarm Cluster上运行.

在我的PHP应用程序中,我需要获取remote_addr($ _SERVER ['REMOTE_ADDR']),其中包含访问我的webapp的客户端主机的真实IP.

但问题是IP通过docker swarm集群通知了nginx.它显示了内部IP,如10.255.0.2,但真正的IP是来自客户端主机的外部IP(如192.168.101.151).

我怎么解决这个问题?

我的docker-compose文件:

version: '3'

services:
  php:
    image: php:5.6
    volumes:
      - /var/www/:/var/www/
      - ./data/log/php:/var/log/php5
    networks:
      - backend
    deploy:
      replicas: 1
  web:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - /var/www/:/var/www/
      - ./data/log/nginx:/var/log/nginx
    networks:
      - backend
networks:
  backend:
Run Code Online (Sandbox Code Playgroud)

我的default.conf(vhost.conf)文件:

server {
    listen          80;
    root            /var/www;
    index           index.html index.htm index.php;

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

    location / {
        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_set_header        X-Forwarded-Proto $scheme;

        try_files   $uri $uri/ /index.php;
    } …
Run Code Online (Sandbox Code Playgroud)

php nginx docker docker-compose docker-swarm

8
推荐指数
2
解决办法
5197
查看次数

为什么我的"Set-Cookie"响应标头没有被翻译成实际的cookie?

我正在使用Java 8,Wildfly 11,Spring 4和Apache 2.4.我有这个Java代码来设置会话cookie

cookie = new Cookie(SESSION_ID_KEY, sessionId);
...
final String domain = request.getServerName().indexOf(".") == -1 ? request.getServerName() : request.getServerName().substring(request.getServerName().indexOf(".") + 1, request.getServerName().length());
if (!StringUtils.equals(domain, "localhost") && !isIpAddress)
{
            cookie.setDomain(domain.indexOf('.') > -1 ? "." + domain : domain);
}   // if
final String contextPath = request.getContextPath() != null && request.getContextPath().endsWith("/") ? request.getContextPath().substring(0, request.getContextPath().length() - 1): request.getContextPath();
cookie.setPath(contextPath);
System.out.println("setting domain " + domain + " and context path:" + contextPath);
response.addCookie(cookie);
Run Code Online (Sandbox Code Playgroud)

我在浏览器中注意到这个cookie没有被创建.然后我看了Postman,注意到没有创建cookie,虽然我看到这些响应标题......

Set-Cookie ?MY.SESSION.ID=10c25010534c4dd3900851ec1dfaebeb; path=/context; domain=.compute-1.amazonaws.com
Set-Cookie ?closeTrialNoteDialog=""; Max-Age=0; …
Run Code Online (Sandbox Code Playgroud)

java cookies spring response setcookie

8
推荐指数
3
解决办法
573
查看次数