Under my domain, say example.com
, I want to have a landing page (which will surface when hitting example.com
), a WordPress blog (which will live at blog.example.com
) and my resume in HTML (which will live at resume.example.com
). Using nginx, I want to have three separate .conf files so that I can bring them up and down separate from each other. The DNS is set up such that “resume” and “blog” are A records for example.com
.
I’m running this on a Linode, so I have full control over DNS and the web server. However, when I try to split the nginx .conf entry for blog.example.com
into a separate file, I get a redirect from blog.example.com
to www.example.com
when I hit this in a web browser.
I know the install of WordPress isn’t the problem, because when all three server entries are in one nginx file, everything works fine. I have also tried creating a separate folder structure for the blog (blogs-available
and blogs-enabled
) then including those folders in the nginx.conf
before the sites-enabled
include, but that didn’t work either. This leaves me with two questions:
-
Does anyone have any suggestions for how to get this to work? Perhaps my DNS settings are incorrect.
-
Does nginx care about the load order or is it ambiguous? If it’s the latter, then I’ll keep everything in one server block and either have them all up or all down.
UPDATE:
nginx.conf for example.com
:
# Blog (WordPress site - blog.example.com)
server {
listen <ip-address>;
server_name blog.example.com;
root /www/blog.example.com/public_html/current;
access_log /www/blog.example.com/logs/access.log;
error_log /www/blog.example.com/logs/error.log;
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php;
}
location ~ .php$ {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME /www/blog.example.com/public_html/current/$fastcgi_script_name;
if ($uri !~ "^/images/") {
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
}
fastcgi_index index.php;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
# redirect example.com to www.example.com
server {
listen <ip-address>;
server_name example.com;
rewrite ^(.*)$ $scheme://www.example.com$request_uri? permanent;
break;
}
# static site - www.example.com
server {
listen <ip-address>;
server_name www.example.com;
# static HTML / soon-to-be Sinatra site
root /www/example.com/public_html/current/public;
access_log /www/example.com/logs/access.log;
error_log /www/example.com/logs/error.log;
}
Note: I don’t have the resume.example.com
site above, but that should be trivial. Also, the above work in any order, so I don’t believe it’s a redirect issue.
** 2nd Update **
On a hunch, I commented out the following in my example.com
conf file:
server {
listen <ip-address>:80;
server_name example.com;
rewrite ^(.*)$ $scheme://www.example.com$request_uri? permanent;
break;
}
And it worked as I expected. Is there something wrong with my non-www -> www redirect rule above?
For 2nd question, nginx config is declarative. The server block order doesn’t matter. So it’s easy to manage each server block in its own .conf file.
UPDATE After seeing the nginx config file
can’t find anything wrong with the nginx conf file. Tried it in my devbox and it worked fine. So it could be from dns setup. One possibility is that you setup “blog” as a CNAME record of “www” (a cname record is an alias). e.g.,
If that’s the case, a web browser will send http://blog.example.com to http://www.example.com.
Also note that dns record could be cached inside your browser, or in your isp dns server. Even if you fix it for a while, the wrong record may still be cached. Try
to obtain some information of the dns setup.