Skip to content

processor.php getHostUrl() does not detect the server port correctly #12969

Closed
@boldhedgehog

Description

@boldhedgehog

When Magento runs using the sandwich hosting set up (nginx-Varnish-nginx), and the backend nginx run on a not standard port (80 nor 443), the base URL is detected wrong in pub/errors/processor.hp

Preconditions

  1. Magento 2.1.7
  2. nginx as the SSL offloader on 80, 443 ports
  3. Varnish used for FPC
  4. nginx as the backend for Varnish listening to not standard port (for example 8081)
  5. PHP-FPM runs Magento
  6. Production mode

Steps to reproduce

  1. Open any media resource that does not exist and is not allowed in var/resource_config.json

Expected result

  1. Magento's 404 page should be displayed in the browser with a link to configured skin CSS.
  2. Base in the HTML code should be generated based on the requested host, for example:
<base href="https://domain.com/errors/default/" />

Actual result

  1. Magento's 404 page should be displayed in the browser with a link to configured skin CSS.
  2. Base in the HTML code is generated based on the requested host AND $_SERVER['SERVER_PORT'] , for example:
<base href="https://domain.com:8081/errors/default/" />
  1. No static assets with relative path can be loaded due to the faulty base

The logic in processor.php should be similar to one in //vendor/zendframework/zend-http/src/PhpEnvironment/Request.php:

  • Try to use HTTP_HOST header
  • If the above is not available, the host is $_SERVER['SERVER_NAME']:$_SERVER['SERVER_PORT']

At this moment also in 2.3-develop branch the server port is always concatenated:

public function getHostUrl()
    {
        /**
         * Define server http host
         */
        if (!empty($_SERVER['HTTP_HOST'])) {
            $host = $_SERVER['HTTP_HOST'];
        } elseif (!empty($_SERVER['SERVER_NAME'])) {
            $host = $_SERVER['SERVER_NAME'];
        } else {
            $host = 'localhost';
        }

        $isSecure = (!empty($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off');
        $url = ($isSecure ? 'https://' : 'http://') . $host;

// ------> should not be the case if the $host is assigned 
// ------> from  $_SERVER['HTTP_HOST']
       if (!empty($_SERVER['SERVER_PORT']) && !in_array($_SERVER['SERVER_PORT'], [80, 433])
            && !preg_match('/.*?\:[0-9]+$/', $url)
        ) {
            $url .= ':' . $_SERVER['SERVER_PORT'];
        }
        return  $url;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fixed in 2.2.xThe issue has been fixed in 2.2 release lineFixed in 2.3.xThe issue has been fixed in 2.3 release lineIssue: Clear DescriptionGate 2 Passed. Manual verification of the issue description passedIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedIssue: Format is validGate 1 Passed. Automatic verification of issue format passedIssue: Ready for WorkGate 4. Acknowledged. Issue is added to backlog and ready for developmentReproduced on 2.1.xThe issue has been reproduced on latest 2.1 releaseReproduced on 2.2.xThe issue has been reproduced on latest 2.2 releaseReproduced on 2.3.xThe issue has been reproduced on latest 2.3 release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions