Url rewriting nginx配置文件重定向到子文件夹

Url rewriting nginx配置文件重定向到子文件夹,url-rewriting,nginx,subdomain,Url Rewriting,Nginx,Subdomain,我目前正试图在两个目录中部署一个网站。尽管有很多关于它的文件,我还是无法获得我想要的行为。这是网站的架构: 主网站页面存储在/opt/www/mainsite/index.html中 第二个webiste(使用CodeIgniter)存储在/opt/www/apps/myapp/index.php中 我希望配置nginx以获得此行为: http中的所有请求都必须重定向到https www.mydomain.com必须指向/opt/www/mainsite/index.html www.my

我目前正试图在两个目录中部署一个网站。尽管有很多关于它的文件,我还是无法获得我想要的行为。这是网站的架构:

  • 主网站页面存储在/opt/www/mainsite/index.html中
  • 第二个webiste(使用CodeIgniter)存储在/opt/www/apps/myapp/index.php中
我希望配置nginx以获得此行为:

  • http中的所有请求都必须重定向到https
  • www.mydomain.com必须指向/opt/www/mainsite/index.html
  • www.mydomain.com/myapp必须指向/opt/www/apps/myapp/index.php
当前,我的配置文件包含:

# redirect http to https
server {
    listen 80;
    rewrite ^(.*) https://$host$1 permanent;
}

# main webiste
server {
    listen 443;

    # ssl elements...  
    root   /opt/www/mainsite;
    index index.html;
    server_name             www.mydomain.com;

    location / {
            try_files $uri $uri/ /index.html;
    }
}
在此基础上,我找到了为CodeIgniter设置配置文件的所有信息。但我不知道如何创建规则,将mydomain.com/myapp指向CodeIgniter文件夹,以及如何配置CodeIgniter以设置正确的配置

有人能帮我吗

提前谢谢

server {
    listen 80;
    listen 443 ssl;
    …
    if ($scheme != "https") {
        rewrite ^ https://$server_name$request_uri? redirect;
    }
    root /opt/www/mainsite/;
    location /myapp {
        root /opt/www/apps/myapp/;
    }
}
您可以将您的
myapp
所需的任何配置放在
myapp
位置中

顺便说一句,由于XSS的考虑,在一个
主机中托管多个独立的应用程序通常不是一个好主意

您可以将您的
myapp
所需的任何配置放在
myapp
位置中

顺便说一句,由于XSS的问题,在一个
主机中托管多个独立的应用程序通常不是一个好主意。

  • 您需要设置CodeIgniter
  • 使用2个服务器块比使用if块进行重定向要好。看
  • 不要使用$host,因为该变量值是从请求的主机头获得的,很容易伪造。始终设置服务器名称指令,并改用该名称
  • 使用“return301”指令比重写要好。节省cpu时间(regex很慢)且易于操作。请注意,302重定向(rewrite…redirect)有副作用,因为302会将所有POST请求转换为GET请求,这在您的情况下是不好的
  • 您不需要在主站点中使用try_文件,因为主站点只提供静态文件。但您可以使用“expires”指令来允许浏览器缓存静态文件
  • 您需要设置CodeIgniter
  • 使用2个服务器块比使用if块进行重定向要好。看
  • 不要使用$host,因为该变量值是从请求的主机头获得的,很容易伪造。始终设置服务器名称指令,并改用该名称
  • 使用“return301”指令比重写要好。节省cpu时间(regex很慢)且易于操作。请注意,302重定向(rewrite…redirect)有副作用,因为302会将所有POST请求转换为GET请求,这在您的情况下是不好的
  • 您不需要在主站点中使用try_文件,因为主站点只提供静态文件。但您可以使用“expires”指令来允许浏览器缓存静态文件

感谢您的回复和所有细节!我的应用程序现在正在工作,但文件夹“资产”不可用,并返回错误404(必须指向/opt/www/apps/myapp/assets,但它不工作)。您知道如何解决此问题吗?为资源添加另一个位置块,以便允许浏览器缓存这些静态文件。。Nginx wiki为此提供了一个网页。感谢您的回复和所有细节!我的应用程序现在正在工作,但文件夹“资产”不可用,并返回错误404(必须指向/opt/www/apps/myapp/assets,但它不工作)。您知道如何解决此问题吗?为资源添加另一个位置块,以便允许浏览器缓存这些静态文件。。Nginx wiki为此提供了一个网页。
server {
    listen 80;
    server_name www.mydomain.com;
    return 301 https://$server_name$request_uri;
 }

 server {
    listen 443;
    server_name www.mydomain.com;
    # ssl elements...  

    location / {
        root   /opt/www/mainsite;
        index index.html;
        expires max;
    }

    location /myapp {
        root /opt/www/apps/myapp;
        # fastcgi module goes here...
    }
}