2

I have created one Django app which has two apps named "api" and "consumer". Now I want to use subdomains for both of this app. Like api.server.com and server.com. I searched online and found django-hosts so I implemented in my localhost and its working fine.

After that I deployed it on AWS EC2 instance and created subdomain in Godaddy and point both root domain and subdomain to my instance IP. Root domain is working fine but when I try to go api.server.com, it shows me default Welcome to Nginx screen. Please help me with this issue.

nginx.conf

server{
    server_name server.com, api.server.com;
    access_log  /var/log/nginx/example.log;

    location /static/ {
        alias /home/path/to/static/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/username/project/project.sock;
    }
}
Rahulsinh
  • 41
  • 1
  • 6
  • 1
    I think its due to `,` in the server_name: check https://nginx.org/en/docs/http/server_names.html. Also, you don't have to use a plugin (django-hosts) to assign different domain names to different paths.: Create 2 different ngnix configs like this:https://stackoverflow.com/a/14492755/1771949 – Rohith Mar 25 '20 at 12:03
  • @Rohith I saw that link but I think its different from mine because I am using gunicorn socket file for this. Can you please let me know how to rewrite using the socket file? – Rahulsinh Mar 26 '20 at 06:39

2 Answers2

2

You don't need the , a simple space will do.

server_name server.com  api.server.com;

Also you can use wildcards, see the documentation.

server_name *.server.com;
yvesonline
  • 4,609
  • 2
  • 21
  • 32
  • Thank you for your code. Now I am able to access my site using the subdomain but @Rohith mentioned that I need to rewrite url for this but I am using gunicorn socket file for this. Can you please let me know how to rewrite using the socket file? – Rahulsinh Mar 26 '20 at 06:41
0

You don't have to use a plugin (like django-hosts) to achieve what you are trying to do. Create 2 different nginx configurations for each subdomain you want to create (server.com and api.server.com), and forward requests from api.server.com to /api URL and request from server.com to /. Following is a basic example.

server.com

server {
    listen 80;

    server_name server.com;
        location / {
            proxy_pass http://127.0.0.1:3000$request_uri;
    }

}

api.server.com

server {
    listen 80;

    server_name api.server.com;
        location / {
            proxy_pass http://127.0.0.1:3000/api$request_uri;
    }

}

I recommend not to depend on 3rd party plugins unnecessarily. Refer https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ for more details.

Rohith
  • 1,301
  • 3
  • 21
  • 31
  • I have already tried this but I am using sock file for "proxy_pass". You solution didn't work for me because of this. – Rahulsinh Mar 26 '20 at 09:23