routing subdomain to subdirectory on nginx

[亡魂溺海] 提交于 2021-02-08 10:28:23

问题


Please help as explicitly as possible. I have set up a domain on a home server running nginx on Ubuntu 15, and I have the dns pointed to it. I can use the domain to access the site and if I append /subdirectory to it, I am able to launch the pages inside the subdirectories. What I am trying to do is get the subdomains to go directly to the correct root. Ie: mysite.com = /index.htm, subdomain.mysite.com = ./subdirectory where files are located. I have tried every suggestion including those popular and those criticized, and either I get an error restarting Nginx or I get a "server not found" error. I´ve tried setting up cname aliases with my DNS server, and that doesn´t work either.

The working config file is below:

    ##

server {


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

        index index.php index.html index.htm;
    root //media/user/ednet/$sub;
    ssl_certificate REMOVED FOR SECURITY
    ssl_certificate_key REMOVED FOR SECURITYY
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!Anull:!md5;

    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
     listen 443 ssl default_server;
     listen [::]:443 ssl default_server;



#   root //media/user/ednet;

    # Add index.php to the list if you are using PHP
    index index.php index.html index.htm;

#=========================Locations=============================

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
#       root /usr/share/nginx/html;
    }

#============================================================PHP=========================================================
    location ~ \.php$ {
        try_files $uri =404;
#       fastcgi_split_path_info ^(.+\.php) (/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
 }



}

==================== I´ve tried proxies, redirects, and everything else I can think of--including reading the instructions in the Nginx manual and the wiki. Thanks


回答1:


A quick "server not found" by the browser suggests that DNS is not set up. Your subdomains need to resolve to your server's public IP address using either:

  • specific CNAME record pointing to the main domain specific A record
  • pointing to the server's IP address wild (catch-all) A record
  • pointing to the server's IP address

Changes to DNS can take many hours to propagate.

An error restarting nginx suggests a syntax error in the configuration file.

Your configuration file contains no obvious syntax errors, but there are a few strange elements:

  • root directive appears twice
  • index directive appears twice
  • root directive has two leading // where usually one will do

You have IPv4 and IPv6 configured which is fine if you have an IPv6 connection.

You have this server marked as a default_server which means that it will be used even if the server_name regex does not match. Therefore, if you present to the server with a subdomain that returns files from the root, this implies that the regex failed to match but the server block was used by default. In which case, check the regex.

In summary, the first priority is to fix the DNS. Nothing will work unless the subdomains resolve to the IP address of the server.



来源:https://stackoverflow.com/questions/34982178/routing-subdomain-to-subdirectory-on-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!