nginx服务器配置:文件夹的子域 [英] nginx server configuration: subdomain to folder

查看:91
本文介绍了nginx服务器配置:文件夹的子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Apache 2迁移到了nginx,但在处理子域控件时遇到了问题. 我想要的是:当请求x.domain.tld时,内部重写为domain.tld/x

I migrated from Apache 2 to nginx and I've got problems to handly my subdomain control. What I want: When x.domain.tld is requested, internally rewrite to domain.tld/x

我遇到的问题是nginx总是通过告诉浏览器重定向到页面来重定向页面.但是我真正想要的是内部执行此操作,就像Apache 2一样. 另外,如果我仅请求x.domain.tld,nginx会返回404.仅当我执行x.domain.tld/index.php

The problem I've got is that nginx always redirects the page by telling the browser to redirect to. But what I really want is to do this internally, like Apache 2 did. Also, if I only request x.domain.tld, nginx returns a 404. It only works when I do x.domain.tld/index.php

这是我的配置:

server {
        listen      80 default;
        server_name _ domain.tld www.domain.tld ~^(?<sub>.+)\.domain\.tld$;

        root /home/domain/docs/;

        if ($sub) {
                rewrite (.*) /$sub;
        }

        # HIDDEN FILES AND FOLDERS
        rewrite ^(.*)\/\.(.*)$ @404 break;

        location = @404 {
                return 404;
        }

        # PHP
        location ~ ^(.*)\.php$ {
                if (!-f $request_filename) {
                        return 404;
                }

                include       /etc/nginx/fastcgi_params;
                fastcgi_pass  unix:/etc/nginx/sockets/domain.socket;
        }
}

谢谢!

推荐答案

在寻找相同问题的解决方案时,我在Google上找到了此问答,因此我想发布最终使用的解决方案.

As I found this Q&A on Google while looking for a solution for the same problem, I wanted to post the solution I finally used.

MTeck的第一个服务器块看起来非常漂亮,但是对于子域部分,您可以简单地执行以下操作:

The first server block by MTeck looks pretty nice, but for the subdomains part you could simply do the following:

server {
  listen 80;
  server_name "~^(?<sub>.+)\.domain\.tld$";

  root /path/to/document/root/$sub;

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

  location ~ \.php {
    include fastcgi_params;
    fastcgi_pass  unix:/etc/nginx/sockets/domain.socket;
  }
}

这使得root配置指令取决于子域.

This makes the root configuration directive dependent on the subdomain.

这篇关于nginx服务器配置:文件夹的子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆