wordpress site in subdomain on nginx - nginx

I'm having trouble setting up a subdomain with nginx and some wordpress sites.
My www.jackalopegames.com domain is working, but I want to set up dev.jackalopegames.com.
Here's the config file in my sites-enabled folder:
server
{
listen 80;
server_name jackalopegames.com www.jackalopegames.com;
include /etc/nginx/fastcgi_php;
root /var/sitefolder;
index index.php;
}
server {
listen 80;
server_name dev.jackalopegames.com;
include /etc/nginx/fastcgi_php;
root /var/devfolder;
index index.php;
}
The first one server_name jackalopegames.com works, but the second one doesn't. I've looked around a bunch and I'm at a loss as to why this isn't working. Any tips would be appreciated!
Update:
I've added the following to my subdomain server {...} with no effect:
location / {
root /var/dev;
index index.php;
rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
if (!-e $request_filename) {
rewrite ^.+/?(/wp-.*) $1 last;
rewrite ^.+/?(/.*\.php)$ $1 last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$
{
root /var/dev;
rewrite ^/.*(/wp-.*/.*\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$ $1 last;
rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js))$/wp-includes/ms-files.php?file=$1 last;
expires 30d;
break;
}
location ~ wp\-.*\.php|wp\-admin|\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/dev$fastcgi_script_name;
}
}

There are a bunch of things that you may or may not have already checked. I had similar issues myself when I first did this. So, to help with just thing thing, I wrote up a walkthrough for how to do it. You can find it here: http://blog.phpadvocate.com/2014/10/setting-up-your-wordpress-blog-as-a-subdomain-with-nginx/

Related

Nginx remove php extension and index from url

Hope someone can help me.
be aware; sorry for possible bad english ;)
I'm trying for hours to setup my configs so my site is accessible without php extension, but will redirect any request with extension and in addition remove the /index from URL.
i have come to the point where every extension will removed and still got parsed by php5-fpm.
here is my current code:
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.php;
<<unrelated Modulecode>>
rewrite ^(.*).php(.*)$ $1$2 permanent; # << forces index to be appended, so not exactly wanted
#rewrite ^/index$ / permanent; >> results in infinity loop for indexpage
# location ~ ^(.*)(?<!(index)).php(.*)$ { # << even don't know how to at least except index...gwarz regex x|
# rewrite ^(.*).php(.*)$ $1$2 permanent;
# }
location / {
try_files $uri $uri/ #phprule;
}
location ~ \.php$ {
try_files $uri $uri/ 404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location #phprule {
rewrite ^(.*)$ $1.php last;
}
}
the uncommented parts are some desperate trys to get it somehow to work x<
so what i want to accomplish:
example.com/index.php => example.com/
example.com/foo.php => example.com/foo
example.com/some/path/foo.php => example.com/some/path/foo
hope you will get the idea.
Probably found the issue or moved on, but for the benefit of those who come along after, these little changes did the trick for me:
location #phprule {
rewrite ^/(.*)$ "/$1.php" last;
}
/ added to the regex, quotes and a forward slash added to the rewrite string.

Nginx deny doesn't work for folder files

I'm trying to restrict access to my site to allow only specific IPs and I've got the following problem: when I access www.example.com deny works perfectly, but when I try to access www.example.com/index.php it returns "Access denied" page AND php file is downloaded directly in browser without processing.
I do want to deny access to all the files on the website for all IPs but mine. How should I do that?
Here's the config I have:
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ #handler; ## If missing pass the URI to front handler
expires 30d; ## Assume all files are cachable
allow my.public.ip;
deny all;
}
location #handler { ## Common front handler
rewrite / /index.php;
}
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9001;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
That is because your deny/allow rule applies to just one location.
Remove that and try:
server {
listen 80;
server_name example.com;
root /var/www/example;
if ($remote_addr != "YOUR.PUBLIC.IP") {return 403;}
...
}
As the test is outside any specific locationblock, it will apply to all cases.
Note also that IF is not evil here since it just "returns".
OK, so I've found the solution. Nginx processes the most exact regex which in this case is the regex for php files. To make the config work all further locations must be defined within / location rule except for #handler (you cannot put under any rule - only as root)
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
index index.html index.php; ## Allow a static html file to be shown first
try_files $uri $uri/ #handler; ## If missing pass the URI to front handler
expires 30d; ## Assume all files are cachable
allow my.public.ip;
deny all;
location ~ .php/ { ## Forward paths like /js/index.php/x.js to relevant handler
rewrite ^(.*.php)/ $1 last;
}
location ~ .php$ { ## Execute PHP scripts
if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
expires off; ## Do not cache dynamic content
fastcgi_pass 127.0.0.1:9001;
fastcgi_param HTTPS $fastcgi_https;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params; ## See /etc/nginx/fastcgi_params
}
}
location #handler { ## Common front handler
rewrite / /index.php;
}
}

Nginx URL rewriting Wordpress

I am running Wordpress with nginx and the URL rules rewriting is over my head. I don't understand why is it not working.
Should be:
https://www.impotencetointimacy.com
But redirects to:
https://www.impotencetointimacy.com/impotencetointimacy.com/faq/
I have removed the .htaccess file.
Here is my nginx config:
server {
listen 66.216.100.111:80;
server_name impotencetointimacy.com www.impotencetointimacy.com;
root /home/mydir;
error_log /home/mydir/error_log;
location / {
index index.php index.html index.htm;
# return 301 https://$host$request_uri;
}
location ~ \.php$ {
root /home/mydir;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/mydir/$fastcgi_script_name;
include fastcgi_params;
}
if (!-e $request_filename)
{
rewrite ^(.+)$ /index.php?q=$1 last;
}
#if (!-e $request_filename) {
# rewrite ^.* /index.php break;
#}
}
Another config is for SSL vhost.
What is going wrong?

Nginx auth_basic and rewrite in subdirectory does not work

I'm trying to add a project to a subfolder of existing webserver with Nginx. Here's my simple config:
server {
listen 80 default_server;
server_name localhost;
root /var/www;
[...]
location = /my-project { return 301 /my-project/; }
location /my-project/ {
alias /var/www/my-project/web/;
index index.php;
location ~ /[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
}
if (-f $request_filename) { break; }
rewrite ^ /my-project/index.php last;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
}
Because there is an rewrite directive inside /control location block, the auth_basic never gets triggered, because Nginx executes rewrites before authentication. In which way should I modify the config, that auth does work?
PS: try_files doesn't seem to work, because it serves files from root (/) webfolder!? When I replace the if and following rewrite with try_files $uri /my-project/index.php?$query_string; I get a 404, because Nginx tries to serve the file /var/wwwindex.php (have a look at missing slash and the root folder /var/www instead of alias).
EDIT 18.09.2013:
As VBart suggests I'm using now the following configuration to get authentication to work
location ~ ^/(?<dir>my-project)(?<path>/.*)$ {
root /var/www/$dir/web;
location ~ ^/[^/]+/control(/|$) {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/htpasswd;
try_files $path /$dir/index.php?$query_string;
}
try_files $path /$dir/index.php?$query_string;
location ~ ^/[^/]+/index\.php$ {
include fastcgi_params;
fastcgi_pass unix:/var/run/fcgi.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
}
Use:
error_page 404 =200 /my-project/index.php;
instead of ugly rewrites:
if (-f $request_filename) { break; }
rewrite ^(.*)$ /my-project/index.php last;
Reference:
http://nginx.org/r/error_page
http://wiki.nginx.org/IfIsEvil
http://wiki.nginx.org/Pitfalls
P.S. try_files doesn't work with alias because of bug: http://trac.nginx.org/nginx/ticket/97, but you can replace alias with root directive: http://nginx.org/r/root

Multisite Wordpress subsite redirect to main site

I had a problem which I created a Multisite Wordpress.
I used Nginx for my web server.
Multisite was enabled on Wordpress which a main site is freelayers.net
When I created a new website everything is work well. I can recive a successful email. But when I try to access the subsites like cantho.freelayers.net. It always redirect me to freelayers.net
I had also add a wildcard record (*) on DNS manager. It looks like:
(IP address /URL) my server IP (Record Type) A/Address
I thinks something wrong in my conf file on Nginx but I can find any solution
This is configuration file for the main site.
server {
listen 9880;
server_name www.freelayers.net;
rewrite ^/(.*) /wp-includes/ms-files.php?file=$1;
root /home/web/freelayers;
index index.html index.htm index.php;
#if (!-e $request_filename) {
# rewrite ^/(.*)$ /index.php?q=$1 last;
#}
access_log /home/web/freelayers/logs/access.log;
error_log /home/web/freelayers/logs/error.log;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php break;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Please give some ideas about this issue! Thanks for any reply!

Resources