简体   繁体   English

Wordpress / wp-admin nginx和apache,尝试下载文件

[英]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: 这是一个自定义的php MVC框架,这是位于根目录中的.htaccess:

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. 根文件夹中有一个名为blog的目录。 This is where a wordpress install sits. 这是安装WordPress的地方。

We had been just using apache for everything and everything worked fine. 我们一直在使用apache进行所有操作,并且一切正常。 However, we have gained more traffic and are now using nginx to server static content, with apache serving everything else 但是,我们获得了更多的流量,现在正在使用nginx来处理静态内容,而apache可以处理其他所有内容

apache config: apache的配置:

<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: Nginx的配置:

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. 您可以转到主页的WordPress页面,博客文章,发表评论等。

However, the only thing that will not work is wordpress' wp-admin 但是,唯一行不通的是wordpress的wp-admin

when we try to go to this page: example.com/blog/wp-admin 当我们尝试转到此页面时:example.com/blog/wp-admin

The browser downloads the source code as wp-login.php 浏览器将源代码下载为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? 在尝试配置Nginx时,我已经简要地看到了这一点,它与index.php有关系吗?

Anyone have any idea. 任何人都有任何想法。

The blog dir, contains all standard wordpress install, including the default .htaccess for wordpress 博客目录,包含所有标准的wordpress安装,包括wordpress的默认.htaccess文件

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. php已安装,就像我说的那样,一切正常,除了wp-admin,它看起来像是wp-login,在这种情况下,nginx / apache尝试下载实际文件。

It could be a mime problem i am looking into it 我正在调查这可能是一个哑剧问题

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. 这将首先测试文件$uri存在,如果存在,则将其提供。 Specifically: 特别:

When you first browse to /wp-admin , WordPress sees that you are not logged in and redirects you to /wp-login.php . 当您首次浏览到/wp-admin ,WordPress会发现您尚未登录,并将您重定向到/wp-login.php Therefore $uri is /wp-login.php , and since that file does indeed exist, nginx goes ahead and serves it up. 因此$uri/wp-login.php ,并且由于该文件确实存在,因此nginx继续进行处理。

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. 这会将所有对PHP资源的请求传递到您的代理。 The "@" is required because try_files needs at least one file parameter; 由于try_files至少需要一个文件参数,因此必须使用“ @”。 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. 我还看到了try_files在野外与类似的伪文件的另一种用法。 I think this could possibly be done with rewrite but couldn't bother to figure it out. 我认为这可以通过rewrite来完成,但不必费心找出来。

I think there could be one of two problems here. 我认为这里可能存在两个问题之一。

Problem A: You don't have PHP installed for some reason. 问题A:由于某种原因,您没有安装PHP。 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. 和/或问题B:您设置的nginx服务器可能没有为PHP设置正确的MIME类型,因此仅假定它是一个文本文件。 I'm not entirely sure, I'm no master at Nginx, but based on my knowledge this is what I could come up with. 我不确定,我不是Nginx的专家,但根据我的知识,这是我能想到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM