Daniil
Daniil

Reputation: 163

Nginx "upstream" directive is not allowed here

I'm trying to set up a Django website with uwsgi and nginx. I created configuration file mysite.conf:

# the upstream component nginx needs to connect to
upstream django {
    server unix:///home/path/to/mysite/mysite.sock;
}

# configuration of the server
server {
    listen      80;
    server_name mydomain.com www.mydomain.com;
    charset     utf-8;
    # max upload size
    client_max_body_size 350M;

    # Django media and static files
    location /media  {
        alias /home/path/to/mysite/media;
    }
    location /static {
        alias /home/path/to/mysite/static;
    }

    # Send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /home/path/to/mysite/uwsgi_params;
    }
}

And it gives this error:

nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/sites-enabled/mysite.conf:2

I don't why it happens. I followed this tutorial: https://tonyteaches.tech/django-nginx-uwsgi-tutorial/

Upvotes: 4

Views: 7454

Answers (3)

Gerlyn M
Gerlyn M

Reputation: 1

in my case, I have added an upstream section inside of the server section, that's a mistake.

server {
listen 80;
server_name example.com;

upstream backend_servers {
  server backend1.example.com;
  server backend2.example.com;
}

location / {
    proxy_pass http://backend_servers;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    }
}

the correct method is to add upstream outside of the server section

upstream backend_servers {
server backend1.example.com;
server backend2.example.com;
}

server {
    listen 80;
    server_name example.com;

location / {
    proxy_pass http://backend_servers;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    }
}

Upvotes: 0

yucer
yucer

Reputation: 5049

This can also happen if your file is being deployed over the directory /etc/nginx/conf.d/ but the default config is including files from http.d instead:

include /etc/nginx/http.d/*.conf;

so try also to deploy it over /etc/nginx/http.d/

Upvotes: 0

Daniil
Daniil

Reputation: 163

The problem was that i included configuration files before http tag. It should be:

http {
    include /etc/nginx/sites-enabled/*;
    ...
}

Upvotes: 2

Related Questions