繁体   English   中英

Nginx重定向主机名+位置

[英]Nginx Redirect hostname+location

我的nginx配置使用server_name _; 以便在单个服务器{}块中处理所有域,并将它们全部定向到同一个代码库,该代码库位于具有单个index.php的单个文件夹中,用于整理域路由。 (这是Web框架在单个群集中与100多个域配合使用的方式-不能更改此设置)。

其中一个站点有2个域。 我们将每种语言分别称为example.com和example.net。 该站点还具有特定于语言的子文件夹,我们将其称为:/ com /和/ net /。

我需要创建一个重定向,以确保仅将语言子文件夹加载到匹配的域中。

例如,有效的请求:

example.com/com/

example.net/net/

任何其他请求都需要基于子文件夹进行重定向:

example.com/net/-> example.net/net/

example.net/com/-> example.com/com/

我不能使用该框架,因为该文件夹包含所有静态文件,因此路由器从不加载。 可以在Nginx配置中完成吗?

谢谢

更新,添加我的配置的简化版本:

server {
    listen 80 default_server;
    server_name _;

    root /my/app/root;
    index index.php index.html;

    location / {
      try_files $uri /index.php;
    }

    #PHP
    location ~ \.php$ {
      try_files $uri = 404;
      [fastcgi stuff]
    }



    location = /favicon.ico {
      access_log off;
      log_not_found off;
    }

    location = /apple-touch-icon.png {
      access_log off; log_not_found off;
    }

    location = /apple-touch-icon-precomposed.png {
      access_log off;
      log_not_found off;
    }
}

我认为每个子文件夹将需要1个位置块(但尚未对其进行测试):

location /com {
  proxy_pass http://upstream:8888/com;
  proxy_set_header Host example.com;
  proxy_pass_request_headers on;
}

location /net {
  proxy_pass http://upstream:8888/net;
  proxy_set_header Host example.net;
  proxy_pass_request_headers on;
}

解决了...

location /com/ {
  if ($host = 'www.example.net') {
    return 301 https://www.example.com$request_uri;
  }

  index index.html;
}

而其他位置/域则相反

暂无
暂无

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

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