简体   繁体   中英

Configure Nginx with a Subdomain

I've done my prior research, but cannot seem to find how to properly configure nginx to accept a subdomain.

I currently have it properly configured for mydomain.com, but not analytix.mydomain.com:

server {
        listen       80;
        server_name     *.mydomain.com;
        access_log /home/ubuntu/virtualenv/mydomain/error/access.log;
        error_log /home/ubuntu/virtualenv/mydomain/error/error.log warn;
        connection_pool_size 2048;

        fastcgi_buffer_size 4K;
        fastcgi_buffers 64 4k;

        root /home/ubuntu/virtualenv/mydomain/homelaunch/;

        location /static/ {
            alias /home/ubuntu/virtualenv/mydomain/homelaunch/static/;
        }

        location / {
            proxy_pass http://127.0.0.1:8001;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
        }
    }

the server_name declaration is accepting <anythinghere>.mydomain.com which is good.

If I access analytix.mydomain.com , it throws a Http 500 default which is fine, because its throwing it from the existing application at mydomain.com

The domain is already propogated to this server I'm trying to access it on.

How can I designate a folder, at a path, to house the contents for analytix.mydomain.com ? I would assume i would require changing an attribute in the nginx conf (as shown above)

Create a new server block where you set the server_name to the desired domain. The normal directory and file structure of nginx looks as follows:

/etc/nginx
|
|---- /sites-available
|     |
|     |---- default.conf
|
|---- /sites-enabled
      |
      |---- default.conf -> ../sites-available/default.conf

You have to create a new file in sites-available with the new server block in it for your sub-domain and create a symbolic link to this new file in sites-enabled . A simple reload of nginx will bring your new server up.

Your new file structure looks as follows:

/etc/nginx
|
|---- /sites-available
|     |
|     |---- analytix.conf
|     |
|     |---- default.conf
|
|---- /sites-enabled
      |
      |---- analytix.conf -> ../sites-available/analytix.conf
      |
      |---- default.conf -> ../sites-available/default.conf

Here are the commands involved to do this very fast directly on your server:

# cd /etc/nginx/sites-available
# cat default.conf > analytix.conf
# editor analytix.conf

Change the line server_name *.mydomain.com; to server_name analytix.mydomain.com .

# ln -s analytix.conf ../sites-enabled/
# nginx -t

Only continue if it says that your configuration is okay (which it should be).

# service nginx restart

That's it (please note that all of the above commands are meant for a Debian based distro and some commands might differ if you use something else).


In order to deliver the contents of a different software on your server you have to change the root directive in your configuration and point it to the document root of the other software.

# editor analytix.conf

Change root /home/ubuntu/virtualenv/mydomain/homelaunch/; to root /path/to/other/software; and reload your nginx.

# nginx -t && service nginx reload

That's it, your new application should be serving now.

尝试更改 server_name:

server_name "~^(?<sub>.+)\.mydomain\.com$";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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