简体   繁体   English

Nginx-如何从域的根目录提供Wordpress服务,又可以从其他目录提供服务?

[英]nginx - how can I serve Wordpress from my domain's root but also serve static content from other directories?

I'm just getting started with administering my own VPS at Digital Ocean and playing with nginx configs for multi-domain sites. 我刚刚开始在Digital Ocean管理我自己的VPS,并使用用于多域站点的nginx配置。

This is what I want to achieve: 这是我要实现的目标:

  • foo.com/ maps to /usr/share/nginx/www/wordpress foo.com/映射到/usr/share/nginx/www/wordpress
  • foo.com/bar maps to /usr/share/nginx/www/bar (if it exists, otherwise try this URL with Wordpress above) foo.com/bar映射到/usr/share/nginx/www/bar (如果存在),否则请使用上面的Wordpress尝试使用此URL)
  • foo.com/baz.jpg maps to /usr/share/nginx/www/baz.jpg foo.com/baz.jpg映射到/usr/share/nginx/www/baz.jpg

Essentially, I want the www directory to just be a static location but also to be able to serve Wordpress from a subfolder (so I don't have to mix all my other files in with Wordpress's folder structure). 本质上,我希望www目录只是一个静态位置,而且还希望能够从子文件夹为Wordpress提供服务(因此,我不必将我的所有其他文件与Wordpress的文件夹结构混合在一起)。

Does this make sense, and is it possible? 这有意义吗,有可能吗?

Here's my nginx config so far: 到目前为止,这是我的Nginx配置:

server {
    listen          80;
    server_name     foo.com;
    root        /usr/share/nginx/www;
    index       index.php index.html index.htm;

    location @wp {
        rewrite ^/(.*) /index.php?q=$1;
    }

    location / {
        root /usr/share/nginx/www/foo;
        # index index.php index.html index.htm;
        try_files $uri $uri/ @wp;

        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
        }
    }

}

Apologies if this sounds stupid, I'm still getting my head around how nginx works and what's possible with it. 抱歉,这听起来很愚蠢,但我仍然想弄清楚nginx的工作原理以及它的可能。

I think you're close, but you need some tweaks, I removed some stuff and modified other 我认为您已经接近了,但是您需要进行一些调整,我删除了一些内容并修改了其他内容

location @wp {
    rewrite ^ /wordpress/index.php?q=$1;
}

location / {
    try_files $uri $uri/ @wp;
}
# add php location

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

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