Accidentally deleted conf nginx filled/etc/nginx/sites-enabled/django
, then filled it with the same configuration settings. got the following error:
Feb 02 12:56:53 solomon nginx[32004]: nginx: [emerg] duplicate upstream "app_server" in /etc/nginx/sites-enabled/django.save:1
Feb 02 12:56:53 solomon nginx[32004]: nginx: configuration file /etc/nginx/nginx.conf test failed
Feb 02 12:56:53 solomon systemd[1]: nginx.service: Control process exited, code=exited status=1
Feb 02 12:56:53 solomon sudo[31990]: pam_unix(sudo:session): session closed for user root
Feb 02 12:56:53 solomon systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
Feb 02 12:56:53 solomon systemd[1]: nginx.service: Unit entered failed state.
Feb 02 12:56:53 solomon systemd[1]: nginx.service: Failed with result 'exit-code'.
Configuration, which worked before for sure. Have I done something incorrectly ?:
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 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/django_project/django_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/django_project/django_project/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;
}
}
If you have other configuration files in the directory(/etc/nginx/sites-enabled/django) with same upstream name as 'app_server ' then you get that duplicate upstream error.
So replace 'app_server' to any other name. run nginx -t to check for any errors, then restart nginx,
If you tape in your terminal cat /etc/nginx/nginx.conf
you will see these two lines:
include /etc/nginx/conf.d/*.conf;
Wich means Nginx load all files .conf (for example if you are using dokku to deploy your app, you will see dokku.conf which contain "include /home/dokku/*/nginx.conf;" itself) and this means loading all /home/dokku/whatever-folder/nginx.conf
include /etc/nginx/sites-enabled/*;
This problem came up in my case because my configuration file was in /etc/nginx/conf.d and I also had a symbolic link to it in /etc/nginx/sites-enabled/. Not sure how nginx loads the files but apparently it was loaded twice. No matter what name I chose for the upstream, I got duplicate error. Deleting symbolic link fixed the problem.
If the same upstream name "app_server" is not found, find the same upstream name with different case,like:
upstream app_server {
server 127.0.0.1:9000
}
upstream App_server {
server 127.0.0.1:9000
}
This may also cause conflicts. It may be the setting of nginx, but I haven't found the document yet
Related
I'm trying to launch a Python webapp, doing the same as I usually do (not that I totally know what I'm doing). Usually I see a "Bad Gateway" when I'm running nginx on the server and I go to the IP in my browser. For whatever reason, I'm just getting timed out right now.
edit: im expecting a Bad Gateway, im getting nothing
Thanks in advance,
I haven't done anything with the default config file...
My etc/nginx/sites-available/default looks like this
server {
listen 80;
server_name ec2-52-36-167-131.us-west-2.compute.amazonaws.com;
access_log /var/log/nginx/test.log;
location / {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
sudo service nginx status looks like this
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-06-19 02:06:09 UTC; 8s ago
Docs: man:nginx(8)
Process: 9149 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
Process: 9161 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 9152 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 9162 (nginx)
Tasks: 2 (limit: 1152)
CGroup: /system.slice/nginx.service
├─9162 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─9163 nginx: worker process
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: Stopped A high performance web server and a reverse proxy server.
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jun 19 02:06:09 ip-172-31-25-6 systemd[1]: Started A high performance web server and a reverse proxy server.
When I curl -I ec2-52-36-167-131.us-west-2.compute.amazonaws.com on the server I get the bad gateway I'm expecting, but on my local machine I get a timeout.
Solved, it was a problem in the AWS security groups.
found it here: nginx website on Ubuntu 18 not loading on public IP
I need to deploy app to prod to subdomain. DNS A-record of app.mysite.com have value of machine. A-record of mysite.com have ip of different computer. Stack: Nginx, Django, Gunicorn.
Nginx works fine on IP, but invokes 400 on subdomain.
I've tried adding proxy_set_header values
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
but it doesn't help.
nginx/sites-enabled/mysite:
(If I change server_name ti IP it wirks fine)
server {
listen 80;
server_name app.mysite.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/mysite;
}
location / {
proxy_set_header Host $host;
include proxy_params;
proxy_pass http://unix:/home/user/mysite.sock;
}
}
settings.py
ALLOWED_HOSTS = [ip of machine,'127.0.0.1', 'app.mysite.com','mysite.com']
I want app to work only at subdomain. How could I achieve it?
Possibly helpful last Nginx process logs
Aug 10 21:23:59 my-machine systemd[1]: Starting A high performance web server and a reverse proxy server...
Aug 10 21:23:59 my-machine systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Aug 10 21:23:59 my-machine systemd[1]: Started A high performance web server and a reverse proxy server.
Aug 10 21:25:09 my-machine systemd[1]: Stopping A high performance web server and a reverse proxy server...
Aug 10 21:25:09 my-machine systemd[1]: Stopped A high performance web server and a reverse proxy server.
After hours of testing and configuration the subdomain started after
sudo systemctl restart gunicorn
in /etc/systemd/system
I stopped all gunicorn workers with:
pkill gunicorn
and then restarted gunicorn, in my particular case is:
PYENV_VERSION=3.5.2 gunicorn -c gunicorn_cfg.py testing_webpage.wsgi --timeout 300 --workers=9 --bind=unix:/opt/peaku_co/run/gunicorn.sock
I had the same error too... Turns out I had turned on Force HTTPS redirect on my DNS provider and yet I was listening on port 80 on my VPS on HTTP without SSL certificate installed.
I fixed by installing an SSL certificate on my VPS and rebooting gunicorn.
When I check the status of Nginx
sudo service nginx status
Aug 01 12:35:07 iz2ze9wve43n2nyuvmsfx5z systemd[1]: Starting The nginx HTTP and reverse proxy server...
....
Aug 01 12:35:07 iz2ze9wve43n2nyuvmsfx5z systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument
Aug 01 12:35:07 iz2ze9wve43n2nyuvmsfx5z systemd[1]: Started The nginx HTTP and reverse proxy server.
It report that "Failed to read PID from file /run/nginx.pid: Invalid argument"
However, my website runs properly,
my custom_nginx.conf
server {
listen 80;
server_name *.example.com 39.105.51.**;
charset utf-8;
location / {
uwsgi_read_timeout 30;
uwsgi_pass 127.0.0.1:8200;
include uwsgi_params;
}
location /media/ {
alias /usr/share/nginx/html/;
autoindex on;
}
location /static/ {
alias /root/forum/dst_static/;
}
}
I could visit www.example.com and 39.105.51.** without error.
Should I ignore it, what kind of error is waiting for me ahead?
It seems likely that you're hitting a known race condition (bug) between Nginx and systemd. It seems to be prevalent on low resource machines, particularly if they only have a single CPU.
Essentially what happens is that systemd is expecting the PID file to be populated, by the time that Nginx has forked. But on low resources machines, that happens before the Nginx fork has had the time to create it. So you get an error logged.
It's nothing more than an annoyance, so there's no need to do anything about it if you don't want. If you'd really like to stop it, then there is workaround documented on the Ubuntu bug report:
mkdir /etc/systemd/system/nginx.service.d
printf "[Service]\nExecStartPost=/bin/sleep 0.1\n" \
> /etc/systemd/system/nginx.service.d/override.conf
systemctl daemon-reload
After applying that, try restarting Nginx and see if it still occurs.
systemctl restart nginx
You should no longer get that message.
This is for learning purpose. I have done the web application with Django + Celery/RabbitMQ. I tried to follow this tutorial. I got everything set until "That’s all for gunicorn.". In sense, my Gunicors runs like what it is describe in the tutorial.
Now I am confused with the NGINX settings.
I have these configurations in /etc/nginx/nginx.conf in its http block.
upstream awesome_app {
server unix:/home/notalentgeek/Downloads/awesome_app/run/gunicorn.sock fail_timeout=10s;
}
server {
listen 8080;
client_max_body_size 4G;
access_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log;
error_log /home/notalentgeek/Downloads/awesome_app/logs/nginx-error.log warn;
location /static/ {
autoindex on;
alias /home/notalentgeek/Downloads/awesome_app/static/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://awesome_app;
break;
}
}
}
Everything else there stay untouched.
And then what should I do after this? The tutorial does not way anything. How can I see my web application? Moreover how can I set NGINX for Docker ready?
EDIT: With Gunicorn is running or not, starting NGINX with sudo service nginx start gives this error.
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has begun starting up.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 nginx[28260]: nginx: [emerg] open() "/home/notalentgeek/Downloads/awesome_app/logs/nginx-access.log" failed (2: N
sep 27 17:23:48 notalentgeek-ThinkPad-X220 nginx[28260]: nginx: configuration file /etc/nginx/nginx.conf test failed
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: nginx.service: Control process exited, code=exited status=1
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: nginx.service: Unit entered failed state.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 systemd[1]: nginx.service: Failed with result 'exit-code'.
sep 27 17:23:48 notalentgeek-ThinkPad-X220 sudo[28225]: pam_unix(sudo:session): session closed for user root
The error and access log need to be created before hand. After this my website runs fine.
A Django application is running on the AWS instance, configured via gunicorn and nginx, it is running well for more than a year, but suddenly, I got 502 bad gateway error, then I saw the below mentioned message in the nginx error log,
2017/05/17 16:18:35 [error] 1040#0: *7460 connect() to unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock failed (111: Connection refused) while connecting to upstream, client: xx.xxxx.xx.xxx, server: xx.xx.xx.xxx, request: "GET / HTTP/1.1", upstream: "http://unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock:/", host: "xx.xx.xx.xxx", referrer: "http://xx.xx.xx.xxx"
my nginx configuration:
server {
client_max_body_size 200M;
listen 80;
listen [::]:80 ipv6only=on;
server_name xx.xx.xx.xxx;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/myserver.crt;
ssl_certificate_key /etc/nginx/ssl/myserver.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/webapps/myproject/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock;
proxy_set_header X-Forwarded-Protocol $scheme;
}
if ($scheme = http){
return 301 https://xx.xx.xx.xxx$request_uri;
}
if ($http_host = pas-cash.com){
return 303 https://xx.xx.xx.xxx$request_uri;
}
}
my gunicorn.conf
description "Gunicorn application server handling myproject"
start on runlevel [6789]
stop on runlevel [!6789]
respawn
setuid ubuntu
setgid www-data
chdir /home/ubuntu/webapps/myproject/myproject
exec /home/ubuntu/webapps/myproject/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock myproject.wsgi:application
After that I restarted the nginx by following command
sudo service nginx restart
After restarting, the application is running well, I can't find what will be the specific reason behind this error, I googled it for this, but I got different types of answer but nothing suitable for me, can you guys please help me out for, why this happened, is there any thing missing in my configuration or what will the common/general reason behind this behavior. It will be very helpful for me, Thanks in advance.
This is caused "suddenly" not because of an nginx error but rather an error with gunicorn or your app (code error, packages not installed etc.). It should be relatively easy to log and fix though.
First try running your app from the server python manage.py runserver and see if you get any issues. The same for ... migrate. Often the issue that production does not work but local does is because of missing packages or missing migrations. Create a requirements.txt file on local and install it on production.
If the error is still there check the gunicorn logs with gunicorn --log-file=- YourApp.wsgi:application . Once all those errors have been corrected run
sudo systemctl status gunicorn.socket
sudo systemctl status gunicorn
And you want to have both active and running. If you start getting a 400 error that is a good sign as it is now a Django error (usually allowed hosts). Turn debug=True to see the exact error from django.
Remember whenever any changes are made to code run
sudo systemctl daemon-reload
sudo systemctl restart gunicorn
Just FYI if none of the above work then you can always check your nginx logs with
sudo tail -30 /var/log/nginx/error.log
try to remove http:// from the proxy_pass in the nginx configuration:
server {
client_max_body_size 200M;
listen 80;
listen [::]:80 ipv6only=on;
server_name xx.xx.xx.xxx;
listen 443 ssl;
ssl_certificate /etc/nginx/ssl/myserver.crt;
ssl_certificate_key /etc/nginx/ssl/myserver.key;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/webapps/myproject/myproject;
}
location / {
include proxy_params;
proxy_pass unix:/home/ubuntu/webapps/myproject/myproject/myproject.sock;
proxy_set_header X-Forwarded-Protocol $scheme;
}
if ($scheme = http){
return 301 https://xx.xx.xx.xxx$request_uri;
}
if ($http_host = pas-cash.com){
return 303 https://xx.xx.xx.xxx$request_uri;
}
}
The reason is that gunicorn is listening on a unix socket (the --bind argument). Then nginx should forward traffic to this socket. http:// stands for a TCP socket in a regular IP:PORT, which is not your case.
There are several reasons for this particular issue, in my case it was because the service was not enabled. After running sudo systemctl enable gunicorm.service fixed it.