简体   繁体   English

Nginx-重写/到子文件夹

[英]Nginx - rewrite / to subfolder

I have a site that use to be on the root level of document root. 我有一个站点曾经位于文档根目录的根目录级别。 I am moving it to a subfolderI in the new document root. 我将其移动到新文档根目录中的子文件夹中。 All the links are absolute (/assets/styles.css, /folder/file.php). 所有链接都是绝对的(/assets/styles.css、/folder/file.php)。 My subfolder would be at example.com/sub/. 我的子文件夹位于example.com/sub/。 Is there any way I can make Nginx rewrite /assets to example.com/sub/assets? 有什么办法可以使Nginx将/ assets重写为example.com/sub/assets?

You should have something like this in your nginx.conf 您的nginx.conf中应该有类似的内容

    location /assets {
        root   /var/www/example.com/documentroot/;
        ...
    }

which told NginX to fetch "assets" from that directory. 它告诉NginX从该目录中获取“资产”。 Unless I'm mistaken, you should simply edit the root folder in "/var/www/example.com/documentroot/sub/". 除非我没有记错,否则您只需在“ /var/www/example.com/documentroot/sub/”中编辑根文件夹。

Or, you had a whole root there: 或者,您在那里有一个完整的根:

    location / {
        root   /var/www/example.com/documentroot/;
        index index.php;
        ...
    }

Again you just add "sub/" to "documentroot/". 同样,您只需将“ sub /”添加到“ documentroot /”。

Finally, if you ONLY want to move "assets", edit or add a "location /assets" configuration stanza in nginx.conf. 最后,如果只想移动“资产”,请在nginx.conf中编辑或添加“ location / assets”配置节。

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

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