WordPress/wp-admin nginx and apache, tries to download file

We have recently setup a new server. It is a custom php MVC framework, this is the .htaccess that sits in the root directory:

Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .(ico|css|jpeg|jpg|gif|png|js|pdf)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [L]
</IfModule>

There is a directory in the root folder called blog. This is where a wordpress install sits.

Read More

We had been just using apache for everything and everything worked fine. However, we have gained more traffic and are now using nginx to server static content, with apache serving everything else

apache config:

<VirtualHost *:8080>
    ServerAdmin example@example.com
    ServerName example.com
    ServerAlias www.example.com example.com
    DocumentRoot /srv/www/example.com/public_html/
    ErrorLog /srv/www/example.com/logs/error.log
    CustomLog /srv/www/example.com/logs/access.log combined

<IfModule mpm_itk_module>
    AssignUserId webadmin www-data example
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    rewritecond %{http_host} ^www.example.com [nc]
    rewriterule ^(.*)$ http://example.com$1 [r=301,nc,qsa]
</IfModule>
</VirtualHost>

nginx config:

server {
    listen 80;
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 80;
    server_name example.com;
    root /srv/www/example.com/public_html;
    index index.php;

    # Add trailing slash to */wp-admin requests.
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;

    location ~* ^.+.(?:js|css|jpe?g|htc|xml|otf|ttf|eot|woff|gif|png|svg|ico|pdf|html|htm)$ {
            access_log off;
            expires 30d;
            tcp_nodelay off;
            open_file_cache max=3000 inactive=120s;
            open_file_cache_valid 45s;
            open_file_cache_min_uses 2;
            open_file_cache_errors off;
    }

    location / {
            try_files $uri @proxy;
    }

    location @proxy {
            include /etc/nginx/proxy_params;
            proxy_pass http://127.0.0.1:8080;
    }

    location ~ /.ht {
            deny all;
    }

}

everything on the blog works right. You can go to the home wordpress page, blog posts, post comments etc.

However, the only thing that will not work is wordpress’ wp-admin

when we try to go to this page: example.com/blog/wp-admin

The browser downloads the source code as wp-login.php

I am not sure why this is happening and i am at a loss. I have seen this before briefly while trying to configure nginx, it had something to do with index.php?

Anyone have any idea.

The blog dir, contains all standard wordpress install, including the default .htaccess for wordpress

php is installed, like i said everything is working fine, except for wp-admin, which looks like it goes to wp-login, in this case nginx/apache tries to download the actual file.

It could be a mime problem i am looking into it

Related posts

Leave a Reply

2 comments

  1. I think this might be your problem:

    location / {
            try_files $uri @proxy;
    }
    

    This will first test if the file $uri exists, and if so, serve it. Specifically:

    When you first browse to /wp-admin, WordPress sees that you are not logged in and redirects you to /wp-login.php. Therefore $uri is /wp-login.php, and since that file does indeed exist, nginx goes ahead and serves it up.

    My solution was to add this, though I’m not sure it’s the best way to do it:

    location ~* .php$ {
        try_files @ @proxy;
    }
    

    This passes all requests to PHP resources to your proxy. The “@” is required because try_files needs at least one file parameter; this assumes no such file will ever actually exist.

    I have seen one other use of try_files with a similar dummy file in the wild. I think this could possibly be done with rewrite but couldn’t bother to figure it out.

  2. I think there could be one of two problems here.

    Problem A:
    You don’t have PHP installed for some reason. The fix is simple, just install it using whatever the installation package for your particular distro is.

    AND/OR Problem B:
    There’s a chance that the nginx server you set up doesn’t have the proper MIME types setup for PHP so it just assumes it is a text file. I’m not entirely sure, I’m no master at Nginx, but based on my knowledge this is what I could come up with.