2

For a multi environment setup I have the following:

https://www.example.com/ => CloudFront => https://www-origin.example.com/ => WordPress FastCGI

This works fine. However I would like to be able to bypass CloudFront for debugging purposes (putting a robots.txt to prevent SEO problems).

https://www-origin.example.com/ => WordPress FastCGI

My problem is that WordPress stores absolute URLS so still returns CDN links for any content. I would like nginx to transparantly rewrite www-origin.example.com to www.example.com including the links in the responses. Any pointers? I've tried setting fastcgi_param HTTP_HOST to no avail.

server {
    listen 80;
    listen [::]:80;
    root /data/html/wordpress;
    index  index.php index.html index.htm;
    server_name  _;
    client_max_body_size 100M;

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

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
}

server {
    listen 80;
    root /data/html/wordpress;
    index  index.php index.html index.htm;
    server_name www-origin.example.com;
    client_max_body_size 100M;

    access_log /var/log/nginx/origin-access.log;
    error_log /var/log/nginx/origin-error.log;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
         include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param HTTP_HOST www.example.com;
         include fastcgi_params;
    }
}

** Update 27-02

For now I got the following. Any comments would be appreciated:

server {
    server_name ~^(?<subdomain>.+)-origin(?<domain>\..+\..+)$;

    listen 80;

    access_log /var/log/nginx/access-origin.log;
    error_log /var/log/nginx/error-origin.log;

    location /test
    {
        root   /sites/$subdomain$domain;
    }


    location / { 
      proxy_pass http://127.0.0.1:80;
      proxy_redirect "https://$subdomain$domain/" "https://subdomain-origin$domain/";
      sub_filter_once off;
      sub_filter "$subdomain$domain" "$subdomain-origin$domain";
      sub_filter_types *;
      proxy_http_version 1.1;
      proxy_set_header Accept-Encoding "";

      proxy_set_header Host $subdomain$domain;

      # include details about the original request
      proxy_set_header X-Original-Host $http_host;
      proxy_set_header X-Original-Scheme $scheme;
      proxy_set_header X-Forwarded-For $remote_addr;
    }
}

server {
    listen 80 default_server;
    listen [::]:80;

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

    root /data/html/wordpress;
    index  index.php index.html index.htm;
    server_name _;

    client_max_body_size 100M;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
DaanV
  • 21
  • 2
  • Interesting question. I would probably clone the server / database, make the change, and test on something that's not your production instance. You can run this on the same server / database or use a spot instance. This probably isn't the most efficient way to do things, but having a test system could be useful. Save any scripts you write and you can bring up / update the test system as required. – Tim Feb 26 '18 at 19:19
  • Tim, thank you so much for thinking with me. I'm not debugging a specific problem and I'm looking for a generic solution that I can deploy for over 50 environments. I'd like to have the possibility to bypass the CDN for several reasons: * Debug CDN settings * Bypass Caching (eg content department changing pages and wants to see result immediately) (Could work around this using rules, but I'd like a clean and generic solution) – DaanV Feb 26 '18 at 22:21
  • Debugging in production isn't too bad, but I really do think a test environment would be beneficial. My AWS t2.nano with 512GB of RAM runs 6 Wordpress websites inc PHP and MySQL, so resources aren't an issue. You might be able to develop scripts to export the prod databases, do a find and replace on the URL, update the development databases. I'd probably have a Wordpress install that had symbolic links to uploads, but I'd copy plugins and such over. However, there may be a way to work around the CDN as you want. Using CloudFlare CDN would be easiest, no need for separate origin domain. – Tim Feb 26 '18 at 23:51
  • There may be WordPress plugins that can rewrite media URLs on the fly. – Michael Hampton Feb 27 '18 at 19:59

0 Answers0