nginx server configuration: subdomain to folder

前端 未结 4 1003
鱼传尺愫
鱼传尺愫 2021-02-02 13:49

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 doma

4条回答
  •  不思量自难忘°
    2021-02-02 14:03

    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.


    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 "~^(?.+)\.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;
      }
    }
    

    This makes the root configuration directive dependent on the subdomain.

提交回复
热议问题