By paimoe

2016-09-05 23:47:17 8 Comments

A situation has come up where server1, running Nginx, will forward all "/" locations to server2, while keeping "/api" and a few others on server1. This is also trying to keep SSL working. Trying to move the WP url from to has the front page load properly, but loading wp-admin gives too many redirects.

Server1 Nginx:

upstream webapp_url {
    server IP:80;

server {
        listen 443 ssl;
        access_log /var/log/nginx/example.log;

        ssl_certificate /etc/nginx/ssl/example.crt;
        ssl_certificate_key /etc/nginx/ssl/server.key;
        ssl_ciphers RC4:HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        location /files/ {
                root /home;
                access_log off;
                expires max;
                if ($request_filename !~* ^.*?\.(jpg)|(png)|(gif)|(pdf)){
                        add_header Content-Disposition: "$request_filename";

        location / {
                # proxy_pass http://site_url/;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection 'upgrade';
                proxy_set_header Host $host;
                proxy_set_header X-Forwarded-For $remote_addr;
                proxy_set_header X-Forwarded-Proto https;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Example "1";
                proxy_pass http://webapp_url/;

This loads the other server fine, homepage and links all work (though mixed content warnings because I'm unable to change it in the admin). WP siteurl and home are both set to the new address.

Server2 Nginx:

server {
    #listen       443 ssl;
    listen 80;
    client_max_body_size 30M;
    error_log /var/log/wordpress/error.log info;
    location / {
        root   /home/wordpress;
        try_files $uri $uri/ /index.php?q=$request_uri;
        index index.php  index.html index.htm;

    #ssl_certificate /etc/nginx/ssl/example.crt;
    #ssl_certificate_key /etc/nginx/ssl/example.key;
    #ssl_ciphers RC4:HIGH:!aNULL:!MD5;
    #ssl_prefer_server_ciphers on;

    error_page  404              /404.html;
    location = /404.html {
        root   /usr/share/nginx/html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;

    location ~ \.php$ {
        root           /home/wordpress;
        fastcgi_pass   unix:/var/run/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        include        fastcgi_params;

Loading /wp-admin/ starts an infinite redirect (to the same url). I've defined it in wp-config.php too:



@Richard Smith 2016-09-06 08:34:32

wp-admin checks to see if the connection is secure, otherwise it redirects to the https version of the same URL. In a situation such as yours, this causes a redirect loop.

You need to tell WordPress that the connection is secure.

I notice that you set the appropriate header on server 1 with:

proxy_set_header X-Forwarded-Proto $scheme;

(In your case, the value of $scheme is hard-wired to https).

However, you also need to pass this to WordPress, in the form of an HTTPS parameter, with a value of on.

This is achieved with a map directive (on Server 2):

map $http_x_forwarded_proto $https_flag {
    default off;
    https on;
server {

(The map directive is placed within the http block. You can place it just above the server block as shown above. See this document for details)

In addition, add another fastcgi_param to pass the HTTPS parameter to WordPress (on Server 2):

location ~ \.php$ {
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param HTTPS $https_flag;

Related Questions

Sponsored Content

27 Answered Questions

[SOLVED] How do I make a redirect in PHP?

  • 2009-04-20 14:13:22
  • Sam
  • 2413937 View
  • 1086 Score
  • 27 Answer
  • Tags:   php redirect

7 Answered Questions

[SOLVED] How can I redirect and append both stdout and stderr to a file with Bash?

3 Answered Questions

[SOLVED] Share Nginx server configuration

  • 2014-06-11 04:04:32
  • Anam
  • 1240 View
  • 3 Score
  • 3 Answer
  • Tags:   php nginx

0 Answered Questions

Cannot get index.php page to display in docker container

  • 2019-02-06 04:42:32
  • sp156
  • 38 View
  • 0 Score
  • 0 Answer
  • Tags:   php docker nginx

2 Answered Questions

[SOLVED] Keycloak Redirect url with nginx is going to http rather than https

  • 2018-04-03 12:02:15
  • Atulya Nair
  • 750 View
  • 2 Score
  • 2 Answer
  • Tags:   nginx jboss

58 Answered Questions

[SOLVED] How do I redirect to another webpage?

2 Answered Questions

nginx change root folder for specyfic url

  • 2013-06-27 07:24:06
  • kabra
  • 11457 View
  • 6 Score
  • 2 Answer
  • Tags:   nginx path root

2 Answered Questions

[SOLVED] Express - req.ip returns

31 Answered Questions

[SOLVED] How to manage a redirect request after a jQuery Ajax call

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 319042 View
  • 898 Score
  • 11 Answer
  • Tags:   node.js nginx concept

Sponsored Content