首页 文章

将root重定向到nginx服务器中的另一个文件夹

提问于
浏览
0

我对nginx很新 . 我以前使用Apache并使用htaccess将root重定向到另一个文件夹 . 现在迁移到nginx . 这是我想要实现的四件事

  • 将http重定向到https,例如http://www.example.com - > https://www.example.com

  • 然后将root重定向到另一个文件夹,但必须将URL重写为example.com而不是example.com/blog

  • php中的所有文件都应在url中显示为html,例如example.com/contact.php - > example.com/contact.html

  • example.com/page.php?content=file - > example.com/file我发现此代码重定向但不知道在哪里插入此代码nginx.conf或任何其他文件?

服务器{

location = / {
    return 301 https://www.example.com/blog;
}

}

如果这些更改是在nginx.conf文件或/etc/nginx/sites-available/example.com文件中进行的,请建议我 .

1 回答

  • 1

    要将HTTP重定向到HTTPS流量,您可以创建另一个服务器块以匹配传入的HTTP和域,然后重写到您的HTTPS服务器块 .
    你把它放在哪个文件中? /etc/nginx/nginx.conf/etc/nginx/sites-available/example.com 都应该被读取(除非你改变了配置)所以它应该没关系,但我个人把这些配置放在 /etc/nginx/sites-available/example.com 中因为我认为它是同一个域的一部分 .

    档案: /etc/nginx/sites-available/example.com

    server {
            listen         80;
            server_name    www.example.com;
            return         301 https://$server_name$request_uri;
     }
    
     server {
            listen         443 ssl;
            server_name    www.example.com;
    
            ...
    
            # your location blocks and redirects here
     }
    

相关问题