I have just installed nginx with php5-fpm and got everything running fine with a single vhost server.
I just went to add a secondary server for a wordpress install (different domain name) and when i go to it, it always redirects to the first vhost that has been working the whole time. The strange part is that in the error logs i can see it failing at loading content from wp-content but its trying to pull it from the first domain...
First domain and one that always works:
server {
listen 80;
server_name domain1.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
# Change these settings to match your machine
listen 443;
server_name domain1.com;
# Everything below here doesn't need to be changed
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /data/www/domain1.com/www/;
index index.html index.htm index.php;
ssl on;
ssl_certificate /data/ssl/domain1.pem;
ssl_certificate_key /data/ssl/domain1.key;
ssl_session_timeout 5m;
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~* \.(?:ico|css|js|gif|inc|txt|gz|xml|png|jpe?g) {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location / { try_files $uri $uri/ @rewrites; }
location @rewrites {
rewrite ^/([^/\.]+)/([^/]+)/([^/]+)/? /index.php?page=$1&id=$2&subpage=$3 last;
rewrite ^/([^/\.]+)/([^/]+)/?$ /index.php?page=$1&id=$2 last;
rewrite ^/([^/\.]+)/?$ /index.php?page=$1 last;
}
location /admin { }
location /install { }
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# The next two lines should go in your fastcgi_params
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Second domain that redirects to first domain but resources are trying to load:
server {
listen 80;
server_name domain2.com;
# Everything below here doesn't need to be changed
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /data/www/domain2.com/public_html/;
index index.html index.htm index.php;
if ($host ~* ^www\.(.*))
{
set $host_without_www $1;
rewrite ^/(.*)$ $scheme://$host_without_www/$1 permanent;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}
location ~* \.(?:ico|css|js|gif|inc|txt|gz|xml|png|jpe?g) {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# The next two lines should go in your fastcgi_params
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
Error log showing attempting to pull the right resource but from the wrong domain:
2012/03/11 18:48:45 [error] 29093#0: *26 open() "/data/www/domain1.com/www/wp-content/themes/minimalist/styles/grey.css" failed (2: No such file or directory), client: 60.225.81.244, server: domain1.com, request: "GET /wp-content/themes/minimalist$
That is because the first server is the default server, which accepts any connection.
I see that you've tried to set
www.domain2.com
with an if statement.www.domain2.com
is treated as a different server.. you can simply start another server with:That should do the trick.