0

I have a wordpress website served by nginx:

server {
  listen 80;
  server_name my_wordpress.example.com;
  root /var/www/wordpress;
  index index.php;
  location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params; 
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param REQUEST_URI $request_url;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
  } 
}

What I want to do is to put one simple website consisting of only one file, let's say index.php to temporarily take over the root url of my domain, while keeping the access to the wordpress website on another url.

What is the best way to achieve this?

I went into the direction of setting a variable when user access the url aiming to the wordpress website:

location /original-site {
  set $request_url $request_uri;
  set $original_site "true"; 
  rewrite ^/original-site$ /; }

if ($original_site = "true") {
  set $request_url /; }

redirecting the root url somewhere else:

if ($original_site != "true") {
  rewrite ^/$ /new_website redirect; }

I'm using the $request_url in php:

fastcgi_param REQUEST_URI $request_url;

I have not been very successful so far, I might find the bugs and finetune this approach, does there exist some more elegant way, how to do this?

ryskajakub
  • 101
  • 1

1 Answers1

0

If you're sure you want to only serve a simple index file when the root of your website is accessed, why don't you add just a location = / for that? Without any variables, rewrite or if (remember it's better to look for a solution without if).

The whole server part could look like:

server {
  listen 80;
  server_name my_wordpress.example.com;
  root /var/www/wordpress;
  index index.php;

  location = / {
      # I'd add an expires 10s; or whatever
      return 302 original-site;
  }

  location = /original-site {
    root /var/www/a_special_other_path; # where you stored the special index file to temporarily serve
    try_files index.php index.html;
  }

  location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params; 
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param REQUEST_URI $request_url;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
  } 
}

This way, if someone tries to access exactly my_wordpress.example.com, he/she will get the index file from the special directory. But all others accesses (to my_wordpress.example.com/anything.php) will be treated by the other location block.

If you wish to temporarily serve more than a simple index.html (e.g.), you can use the same parameters in the location = / block as in your location ~ \.php$ block.

And once you're done, just comment out the location = / block you're back to the initial configuration.

zezollo
  • 430
  • 1
  • 4
  • 10
  • yes, that's probably part of the solution, my question is how do I serve the original root '/' renamed to let's say '/original-site' - I'd like to send it to php-fpm with request_url - '/' so I need it set in the nginx from otherwise it would serve the new site '/' – ryskajakub Mar 15 '16 at 19:54
  • Hum, not sure to understand exactly what you want. I edited my answer to let the accesses to root (e.g. my_wordpress.example.com/) to my_wordpress.example.com/original-site; but you seem to want to redirect my_wordpress.example.com/$uri to my_wordpress.example.com/original-site/$uri, don't you? (then my needs to be adapted again: you put the fastcgi stuff in the `location = /original-site` block ). I don't understand how you intend to access your website then (what url for the admin?) (certainly you could map something depending on your IP). – zezollo Mar 15 '16 at 20:35