I'm using vagrant and virtualbox for my Django environment. The django environment uses nginx. Everything works fine except intermittently I'll see 502 bad gateway errors. When these errors happen, there is nothing in nginx access.log or error.log. Here are my configurations
Vagrant file private network
config.vm.network "private_network", ip: "192.168.33.10"
nginx.conf
server {
listen 80 default_server;
server_name _;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location / {
proxy_set_header Host 192.168.33.10;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8000;
}
}
I'm not sure how to debug or fix this issue. Any ideas?
You can try python manage.py runserver 192.168.33.10:8000 since Django defaults your ip to 127.0.0.1 and nginx might have problems with that.
Related
I'm trying to configure my app to run on ec2 with some difficulty.
It's a multi container app built with docker-compose consisting of django, drf, channels, redis gunicorn, celery and nuxt for frontend.
I've an instance running and can SSH to the instance and install the relevant packages, docker nginx docker-compose etc.
What I can't do is edit my app.conf nginx file to use the public ip 33.455.234.23 (example ip)
To route the backend, rest and frontend.
I've created and app.conf nginx file which works fine local but when I try edit the nginx files
after install to configure my app to the public ip's I run into errors.
The error I have when writing my config is
2020/11/13 01:59:17 [emerg] 13935#0: "http" directive is not allowed here in /etc/nginx/default.d/app.conf:3
nginx: configuration file /etc/nginx/nginx.conf test failed
This is my nginx config
worker_processes 1;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
client_max_body_size 100m;
upstream asgiserver {
server asgiserver:8000;
}
upstream nuxt {
ip_hash;
server nuxt:3000;
}
server {
listen 80 default_server;
server_name localhost;
location ~ /(api|admin|static)/ {
proxy_pass http://asgiserver;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
}
location /ws/ {
proxy_pass http://asgiserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
# proxy_redirect off;
}
location / {
proxy_pass http://nuxt;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Host $host;
}
}
}
What am I doing wrong here? What to I need to do in order to have my app running via reverse proxy on my ec2 public address?
It looks like this configuration file isn't treated as the main configuration file by nginx but being included from the main main configuration file /etc/nginx/nginx.conf which in turn already contains the http block with the include directive within, something like
include /default.d/*.conf;
Check this, if it is true, remove everything except upstream and server configuration blocks from the /etc/nginx/default.d/app.conf file. Move the client_max_body_size directive inside the server block.
If I have running my containers on the server side and everything is okay,for accessing route on the browser should I put ip of nginx container with domain in etc/host or it have to work without that?
my nginx.config
server {
listen 80;
server_name my_domain;
root /code/;
error_log /var/log/nginx/new_error.log debug;
location / {
proxy_pass http://web:8000/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /staticfiles/ {
alias /code/staticfiles/;
}
location /mediafiles/ {
alias /code/mediafiles/;
}
}
where web is my docker-container with gunicorn running
If your nginx container has the ports expose using the flag -p <host_port>:<container_port> you can access to nginx service adding the domain in your /etc/hosts file pointing to the ip of your localhost, however if you didn't use this flag you need pointing to the ip of your nginx container. What's the difference?...when you expose the ports you can use the service even outside of the host where the container was deployed.
I hope help you.
I am setting up a domain for my Django/Gunicorn/Nginx server. It works fine with IP address instead of domain name in server_name but when I add domain name it redirects to default Ubuntu Nginx page. My Nginx file looks like this (please note that I replaced my domain with example.com):
Path : /etc/nginx/sites-available/projectname
server {
listen 80;
server_name example.com;
return 301 $scheme://www.example.com$request_uri;
}
server {
listen 80;
server_name www.example.com;
client_max_body_size 4G;
location = /favicon.ico {access_log off; log_not_found off;}
location /static/ {
root /path/to/static/dir;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unix:/path/to/gunicorn.sock;
}
}
I have run the command sudo nginx -t and sudo service nginx restart but no effect. Please let me know if I am doing anything wrong.
1- see main nginx.conf how include all config files. if it is including site-enabled path then go to path and see is a shortcut to config file of this site under site available?
or if all sites are enabled in nginx config file include directly available
include /etc/nginx/sites-available/*;
2-mix two server define code once and with rule forward non www to with www
3-if not work check dns config problem and see result from inside of server via putty not from outside of server with browser to see it is nginx problem or dns config problem.
note: changing dns name servers taken some hours to work and effect on clients.
I have django, nginx, and gunicorn installed on a web server.
Nginx listens on port 80
Gunicorn runs django project on port 8000
This works fine. If I go to www.mysite.com:8000/myapp/ the django application comes up OK. But what if I want users to go to www.mysite.com/myapp/ to view the django application? I don't think getting rid of Nginx is the answer, and I'm hoping I missed some configuration tweak i can apply to make this work.
Any advice is appreciated.
You can use the following configuration, so you can access your website normally on port 80:
this is your nginx configuration file, sudo vim /etc/nginx/sites-available/django
upstream app_server {
server 127.0.0.1:9000 fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 250M;
server_name _;
keepalive_timeout 15;
# Your Django project's media files - amend as required
location /media {
alias /home/xxx/yourdjangoproject/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/xxx/yourdjangoproject/static;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
and configure gunicorn as
description "Gunicorn daemon for Django project"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectadly trigger a respawn
respawn
setuid yourdjangousernameonlinux
setgid yourdjangousernameonlinux
chdir /home/xxx/yourdjangoproject
exec gunicorn \
--name=yourdjangoproject \
--pythonpath=yourdjangoproject \
--bind=0.0.0.0:9000 \
--config /etc/gunicorn.d/gunicorn.py \
yourdjangoproject.wsgi:application
No, getting rid of nginx is definitely not the answer. The answer is to follow the very nice documentation to configure nginx as a reverse proxy to gunicorn.
I'm trying to set nginx with gunicorn but I keep getting the "Welcome to nginx!" page. I am able to successfully listen to other ports (like 8080) but port 80 does not work at all.
server {
listen 80;
server_name host.ca www.host.ca;
access_log /var/log/nginx/example2.log;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://127.0.0.1:8000;
}
}
I'm running the server as root.
I can't seem to see anything running in port 80.
Diagnosing the Problem
Make sure to check your logs (likely /var/log/nginx or some variant).
Check to see what might be hogging port 80
netstat -nlp | grep 80
Sites-enabled, port hogging
Then, make sure you have the Django site enabled in sites-enabled. Delete any old symlinks if you created one first.
rm /etc/nginx/sites-enabled/django
ln -s /etc/nginx/sites-available/django /etc/nginx/sites-enabled/django
Double check your /etc/nginx/nginx.conf to make sure it's loading sites-enabled and not loading some other default.
http {
...
include /etc/nginx/sites-enabled/*;
}
After you do all this, shut down and restart the nginx service.
Either service nginx restart or service nginx stop && service nginx start