Cannot reach nginx on ubuntu AWS EC2 server - amazon-web-services

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

Related

Nginx on EC2: Unable to load the default web page but the service is active

I have installed the Nginx in my ec2 machine
The ubuntu version is
Distributor ID:Ubuntu
Description:Ubuntu 20.04.3 LTS
Release:20.04
Codename:focal
I have installed using the following command
sudo apt install Nginx
After that, I can able to see that the Nginx service is up and running
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 Sun 2022-06-19 07:27:28 UTC; 9min ago
Docs: man:nginx(8)
Process: 4938 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 4940 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 4941 (nginx)
Tasks: 3 (limit: 1116)
Memory: 4.9M
CGroup: /system.slice/nginx.service
├─4941 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─5003 nginx: worker process
└─5004 nginx: worker process
Jun 19 07:27:28 ip-172-31-42-178 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 19 07:27:28 ip-172-31-42-178 systemd[1]: Started A high performance web server and a reverse proxy server.
But when i access the ip of the instance i m getting the error site can't be reached
Security rule groups is added for port 80 and 443.
SSH is active on port 22.
Checked the var/log/nginx but seems all files are empty.
UPDATE
When I check the ufw status using the command sudo ufw status i can see that Status: inactive
But not sure if I enabled the ufw via sudo ufw allow 'Nginx HTTP' it will impact the current security rule group settings.

Django + Nginx on subdomain Bad request 400

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.

Django deploy Gunicorn and NginX

While trying to finally deploy my NEW rover web app a problem occured. I already configured Gunicorn and bind it to 0.0.0.0:80 (with wsgi). Website loads, but without any images, css, javascript files (even "admin" site is without any style). Postgres is installed and running fine. I already did
python manage.py collectstatic
python manage.py migrate
I watched tons of tutorials how to do it. But still after I try to run:
systemctl status nginx.service
i get an error:
Job for nginx.service failed because the control process exited with error code.
See "systemctl status nginx.service" and "journalctl -xe" for details.
#systemctl status nginx.service output:
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since śro 2018-11-28 02:57:12 CET; 1min 13s ago
Process: 14721 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=2)
Process: 25481 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
Process: 25477 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 10279 (code=exited, status=0/SUCCESS)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[25481]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
#journalctl -xe output:
sshd[5669]: Received disconnect from 181.15.216.20 port 42140:11: Bye Bye [preauth]
sshd[5669]: Disconnected from 181.15.216.20 port 42140 [preauth]
sudo[5773]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/systemctl restart nginx
sudo[5773]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)
systemd[1]: Stopped A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has finished shutting down
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has finished shutting down.
systemd[1]: Starting A high performance web server and a reverse proxy server...
-- 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.
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address aginx[5957]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
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.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
sudo[5773]: pam_unix(sudo:session): session closed for user root
sudo[6331]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/journalctl -xe
sudo[6331]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)lready in use)
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx[5957]: nginx: [emerg] still could not bind()
systemd[1]: nginx.service: Control process exited, code=exited status=1
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.
systemd[1]: nginx.service: Unit entered failed state.
systemd[1]: nginx.service: Failed with result 'exit-code'.
sudo[5773]: pam_unix(sudo:session): session closed for user root
sudo[6331]: djangosu : TTY=pts/5 ; PWD=/home/djangosu ; USER=root ; COMMAND=/bin/journalctl -xe
sudo[6331]: pam_unix(sudo:session): session opened for user root by djangosu(uid=0)
/etc/systemd/system/gunicorn.socket file:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
/etc/systemd/system/gunicorn.service file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=djangosu
Group=djangosu
WorkingDirectory=/home/djangosu/website/rover-Project
ExecStart=/home/djangosu/website/rover-env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
rover.wsgi:application
[Install]
WantedBy=multi-user.target
And "rover" file (the same as name of project) in sites-available (I already linked it to sites-enabled):
server {
listen 80;
server_name :333 ;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/.../website/rover/rover;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
gunicorn.sock exists in /run/ dir.... Any suggestions ?
Release note:
Distributor ID: Ubuntu
Description: Ubuntu 16.04.5 LTS
Release: 16.04
Codename: xenial
nginx is telling you, it cannot bind to port 80 as some other process is already listening on this port. You can see this in your log output here:
nginx[5957]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
To check which process is listening on this port you could use netstat or better ss:
~# ss -naptu state listening | grep :80
tcp 0 128 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=6824,fd=4),("nginx",pid=6823,fd=4))
~# netstat -tulpen | grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 136280382 6823/nginx: master
The last column will show the process name and the pid of the process listening on the port.

How can I run Gunicorn with NGINX (I want to move from Django development to test my production)?

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.

ngnix - duplicate upstream "app_server" in /etc/nginx/sites-enabled/django

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