nginx server configuration: subdomain to folder

前端 未结 4 1000
鱼传尺愫
鱼传尺愫 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 "~^(?<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;
      }
    }
    

    This makes the root configuration directive dependent on the subdomain.

    0 讨论(0)
  • 2021-02-02 14:07

    You should take a look at http://wiki.nginx.org/IfIsEvil. You're doing a whole lot wrong in this configuration file.

    server {
        server_name domain.tld www.domain.tld;
    
        location / {
            try_files $uri /index.php;
        }
    
        location ~ \.php {
            include fastcgi_params;
            fastcgi_pass  unix:/etc/nginx/sockets/domain.socket;
        }
    }
    
    server {
        server_name "~^(?<sub>.+)*\.(?<domain>.*)$";
        return 301 $scheme://$domain/$sub$request_uri;
    }
    

    If what you want is to keep that internal, you won't be able to rewrite it. By definition, a cross site rewrite needs to be sent back to the browser. You'll have to proxy the request.

    server {
        server_name "~^(?<sub>.+)*\.(?<domain>.*)$";
        proxy_pass http://$domain/$sub$request_uri;
    }
    

    You should read the Nginx wiki. All of this is explained in depth.

    0 讨论(0)
  • 2021-02-02 14:14

    This will work for www also.

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        index index.php index.html index.htm index.nginx-debian.html;
        server_name ~^www\.(?P<sub>.+)\.domain\.com$ ~^(?P<sub>.+)\.domain\.com$;
        root /var/www/html/$sub;
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }
    }
    
    0 讨论(0)
  • 2021-02-02 14:25

    I spent hours beating my head against the wall and this is what works for me

    server {
        listen       80;
    
        server_name ~^(?P<sub>.+)\.example\.com$; #<-- Note P before sub, it was critical for my nginx
        root /var/www/$sub; #<-- most important line cause it defines $document_root for SCRIPT_FILENAME
    
        location / {
                index index.php index.html; #<-- try_files didn't work as well
        }
    
        location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9000; #<-- probably you have another option here e.g. fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index  index.php;
                fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
                include        fastcgi_params;
        }
    }
    
    0 讨论(0)
提交回复
热议问题