nginx+wordpress rewrite rules issues

March 12, 2014 7.1k views
Hello I think I've run into a bad written configuration files issue, but first of all my configuration: Current config: Debian nginx php php-fpm APC MariaDB wordpress Cause of the problem encountered: after installing all the said components, the wordpress site was working well, but the url displayed the IP as the domain name of my site instead of the correct "mydomain.tld" or "www.mydomain.tld". I managed to solve this putting the following rewrite rule in the main config file of my site in /sites-available server { server_name _; rewrite ^ $scheme://www.mydomain.tld$request_uri last; } BUT, having this rule active locks me out of the wordpress administration page (mydomain.tld/wp-admin/) in a way that seems to be that this rule triggers a loop during the authentication process at "mydomain.tld/wp-login.php" page redirecting at the same page (wp-login.php). I'd say im new in nginx managing and i want to figure out what the #?@! is wrong thanks for all suggestions Mars
2 Answers
Shouldn't need a rewrite rule. Have you tied something like this:

server_name example.org www.example.org;
instead of
server_name _; ?
Of course, that entry was already there before trying to solve the url-IP/domain issue having the sites-available configuration file as follow:

/etc/nginx/sites-available/mydomain.tld
--------------------------------------------------------
#server {
# server_name _;
# rewrite ^ $scheme://www.mydomain.tld$request_uri last;
#}

server {
#listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

root /usr/share/nginx/www/mydomain.tld;
index index.html index.htm index.php;

include global/common.conf;
include global/wordpress.conf;

server_name mydomain.tld www.mydomain.tld;

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
}
--------------------------------------------------------
(he rewrite-trick is currently commented)


For completeness ill add the included configuration files, may this will help.

/etc/nginx/global/wordpress.conf
------------------------------------------------
location / {
try_files $uri $uri/ /index.php?$args;
}

location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}

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

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
------------------------------------------------


/etc/nginx/global/common.conf
---------------------------------------------
listen 80;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

error_page 404 /404.html;

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

location ~ /\. {
deny all;
}

location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires 30d;
}
---------------------------------------------


Have another answer? Share your knowledge.