Nginx反向代理到运行PHP应用程序的远程Nginx服务器

时间:2020-03-24 12:56:22

标签: php nginx reverse-proxy nginx-reverse-proxy

我已使用以下配置设置了在RHEL服务器上运行的PHP应用程序,并对nginx.conf文件进行了一些更改,该应用程序运行良好。

magento.conf

upstream fastcgi_backend {
  server   unix:/run/php-fpm/magento.sock;
}
server {
    listen 80;
    set $MAGE_ROOT /var/www/magento2;
    set $MAGE_MODE developer;
    access_log /var/log/nginx/magento-access.log;
    error_log /var/log/nginx/magento-error.log;
    include /var/www/magento2/nginx.conf.sample;
}

我有另一台nginx服务器,我试图将其用作远程服务器上运行的PHP应用程序的反向代理。我面临的问题是,当我从Nginx IP加载网站时,浏览器中的URL更改为PHP应用程序服务器的IP。

我希望IP与Nginx服务器保持相同,而不是更改为PHP Application Server的IP

反向代理的配置文件如下 reverse.conf

server {
  listen 80;
  #server_name mydomain.com;
    location / {
      access_log off;
      proxy_pass http://10.128.0.10:80;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_redirect     off;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

请帮助我。谢谢

2 个答案:

答案 0 :(得分:0)

没有Lua,您可以尝试将proxy_redirect off;行编辑为proxy_redirect http://10.128.0.10:80 http://my.domain.com:80;

也许在行的末尾加一个斜杠和/或在它的开头重复https(端口443而不是80)。

https://unix.stackexchange.com/a/291007/239596

答案 1 :(得分:0)

与Lua:

server
{
    listen 80;
    # server_name example.com;

    # resolver 127.0.0.11:53;  # Docker-DNS. Needed for proxy_pass with variables inside!

    set $MY_SCHEME http;
    set $MY_HOST example.com;
    set $target "${MY_SCHEME}://${MY_HOST}";

    location /
    {
        rewrite_by_lua_block
        {
            local map = {
                GET = ngx.HTTP_GET,
                POST = ngx.HTTP_POST,
            }
            ngx.req.read_body()
            local res = ngx.location.capture('/example123' .. ngx.var.request_uri, {
                method = map[ngx.var.request_method],
                body = ngx.var.request_body
            })
            -- Detect/change redirect...
            local redirect_target = res.header.Location
            if redirect_target and res.status > 300 and res.status < 309 then
                -- ngx.log(ngx.ALERT, 'redirect_target: '..redirect_target)
                local redirect_target_changed, n, err = ngx.re.gsub(redirect_target, '^https?[:][/][/]10[.]128[.]0[.]10', ngx.var.target)
                -- ngx.log(ngx.ALERT, 'redirect_target_changed: '..redirect_target_changed)
                return ngx.redirect(redirect_target_changed, 303)
            elseif res.status == 500 then
                return ngx.exit(500)
            else
                ngx.exec('@example123_normal')
                return ngx.exit(ngx.HTTP_OK)
            end
        }
    }

    location ~ ^/example123(.*)$
    {
        access_log off;
        proxy_pass http://10.128.0.10:80$1$is_args$args;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect http://10.128.0.10/ $target/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
    }

    location @example123_normal
    {
        access_log off;
        proxy_pass http://10.128.0.10:80$request_uri;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_redirect http://10.128.0.10/ $target/;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

完全未经测试。而且,如果您不能使用Lua和RegEx,它会变得很困难。