nginx proxy_pass does not take affect - nginx

I want to deploy an flask app and followed a tutorial to get this done using nginx.
As the tutorial states I do as follows:
sudo nano /etc/nginx/sites-available/app
this file contains:
server {
listen 80;
server_name server_domain_or_IP;
location / {
include proxy_params;
proxy_pass http://unix:/home/pi/Desktop/python_scripts/internetdisplay/app.sock;
}
}
A systemd Unit service was created and is succesfully running. This created the app.sock file in the 'internetdisplay' directory. Systemctl status app.service results:
● app.service - Gunicorn instance to serve myproject
Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-11-10 21:16:49 CET; 16h ago
Main PID: 438 (gunicorn)
Tasks: 4 (limit: 2200)
Memory: 46.4M
CGroup: /system.slice/app.service
├─438 /usr/bin/python2 /usr/bin/gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi:app
├─679 /usr/bin/python2 /usr/bin/gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi:app
├─681 /usr/bin/python2 /usr/bin/gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi:app
└─682 /usr/bin/python2 /usr/bin/gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi:app
Nov 10 21:16:49 raspberrypi systemd[1]: Started Gunicorn instance to serve myproject.
Nov 10 21:16:57 raspberrypi gunicorn[438]: [2019-11-10 21:16:57 +0000] [438] [INFO] Starting gunicorn 19.9.0
Nov 10 21:16:57 raspberrypi gunicorn[438]: [2019-11-10 21:16:57 +0000] [438] [INFO] Listening at: unix:app.sock (438)
Nov 10 21:16:57 raspberrypi gunicorn[438]: [2019-11-10 21:16:57 +0000] [438] [INFO] Using worker: sync
Nov 10 21:16:57 raspberrypi gunicorn[438]: [2019-11-10 21:16:57 +0000] [679] [INFO] Booting worker with pid: 679
Nov 10 21:16:57 raspberrypi gunicorn[438]: [2019-11-10 21:16:57 +0000] [681] [INFO] Booting worker with pid: 681
Nov 10 21:16:57 raspberrypi gunicorn[438]: [2019-11-10 21:16:57 +0000] [682] [INFO] Booting worker with pid: 682
Then I link to sites-enabled and restart nginx:
sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled
sudo systemctl restart nginx
But surfing to http://localhost leads to an "this site can't be reached" error

It sounds like your location block is not set up correctly to find your resourses.
I assume that this is not the location of your unix socket:
/home/tasnuva/work/deployment/src/app.sock
Check the following:
systemd unit file is creating a socket in the expected location
the daemon is indeed running and the socket file exists
your nginx config is pointing to the correct socket file.
If none of this tells you anything, please update your question with appropriate error log entries.

Related

Is there a way to resolve an issue with nginx status Active: inactive (dead) problem in CENTOS

This is what it brings when you check nginx status
[root#ttproxyapp conf.d]# service nginx status
Redirecting to /bin/systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
sudo systemctl stop apache2
sudo systemctl start nginx
should work since looks like the port is in use (with apache probably)
It realy looks like your NGINX ins't running. nginx -s reload is just working if there is a running instance and therfore a PID-file.
Please check the result of the following command.
[root#localhost conf.d]# sudo ps -elf | grep nginx
Should be something like
1 S root 88262 1 0 80 0 - 13143 sigsus 23:47 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
5 S nginx 88263 88262 0 80 0 - 13257 ep_pol 23:47 ? 00:00:00 nginx: worker process
0 R root 88265 69227 0 80 0 - 28178 - 23:47 pts/1 00:00:00 grep --color=auto nginx
You need at least! the master process and the worker process! If there is no process start your instance by typing
sudo /bin/systemctl start nginx.service OR sudo service nginx start
Check your processlist after running the command.
After you have started the NGINX service there should be PID-file located at
/var/run/nginx.pid and sudo systemctl status nginx.service should printout something like
[root#localhost conf.d]# systemctl status nginx.service
● nginx.service - NGINX Plus - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-03-14 23:47:13 EDT; 5min ago
Docs: https://www.nginx.com/resources/
Process: 88232 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 88260 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 88251 ExecStartPre=/usr/libexec/nginx-plus/check-subscription (code=exited, status=0/SUCCESS)
Main PID: 88262 (nginx)
Tasks: 2
Memory: 1.7M
CGroup: /system.slice/nginx.service
├─88262 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─88263 nginx: worker process
Mar 14 23:47:13 localhost.localdomain systemd[1]: Starting NGINX Plus - high performance web server...
Mar 14 23:47:13 localhost.localdomain systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Mar 14 23:47:13 localhost.localdomain systemd[1]: Started NGINX Plus - high performance web server.
I am running NGINX Plus but it doesn't matter in this case.
there is maybe an apache server running. you must stop that apache server order run the Nginx server.
sudo systemctl stop apache2
sudo systemctl start nginx

Nginx is not proxying Gunicorn

I'm trying to deploy my Flask project using Gunicorn and Nginx but I'm still struggling.
/etc/systemd/system/gunicorn3.service
[Unit] Description=Gunicorn service After=network.target
[Service]
User=www-data
Group=adm
WorkingDirectory=/home/project/
Environment="PATH=/home/project/env/bin"
ExecStart=/home/project/env/bin/gunicorn --workers 3 --bind unix:cima.sock -m 007 run:app
[Install] WantedBy=multi-user.target
Then I check if there's any problem, everything seems OK
>>sudo systemctl status gunicorn3
gunicorn3.service - Gunicorn service
Loaded: loaded (/etc/systemd/system/gunicorn3.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2020-01-17 20:25:01 UTC; 18min ago
Main PID: 18451 (gunicorn)
Tasks: 4 (limit: 4915)
CGroup: /system.slice/gunicorn3.service
├─18451 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
├─18453 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
├─18454 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
└─18456 /home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cimaenv/bin/python3 /home/acelerathon_cima_grupo_3/chatbo
Jan 17 20:25:01 acelerathon-cima-grupo-3 systemd[1]: Started Gunicorn service.
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18451] [INFO] Starting gunicorn 19.9.0
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18451] [INFO] Listening at: unix:cima.sock (18451)
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18451] [INFO] Using worker: sync
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18453] [INFO] Booting worker with pid: 18453
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18454] [INFO] Booting worker with pid: 18454
Jan 17 20:25:01 acelerathon-cima-grupo-3 gunicorn[18451]: [2020-01-17 20:25:01 +0000] [18456] [INFO] Booting worker with pid: 18456
/etc/nginx/sites-enabled/project
server {
listen 80;
server_name 127.0.0.1;
location / {
include proxy_params;
proxy_pass http://unix:/home/acelerathon_cima_grupo_3/chatbot-grupo-3/chatbot-cima/chatbot-cima-back/cima.sock;
}
}
And then I checked for errors.
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
So both services are OK, now when I access to http://localhost I see the Nginx Home Page, not my Flask app home page
I'm not sure why is not proxying
Add localhost to server_name directive in Nginx. Nginx use this directive to match server block for each request and in your case localhost doesn't match 127.0.0.1 so it's server by default server.
May be this example will help to understand:
server {
listen 127.0.0.1:80;
server_name 127.0.0.1;
return 200 "At 127.0.0.1\n";
}
server {
listen 127.0.0.1:80;
server_name localhost;
return 200 "At localhost\n";
}
> curl -4 127.0.0.1:80
At 127.0.0.1
> curl -4 localhost:80
At localhost

Serving API via Flask / Gunicorn / Nginx: Connection refused

I'm having trouble getting gunicorn and Nginx to work together and allow me to offer a simple API via flask:
Locally, running gunicorn and getting responses from the server works fine:
gunicorn wsgi:app (start server)
[2019-06-11 23:12:48 +0000] [14615] [INFO] Starting gunicorn 19.9.0
[2019-06-11 23:12:48 +0000] [14615] [INFO] Listening at: http://127.0.0.1:8000 (14615)
[2019-06-11 23:12:48 +0000] [14615] [INFO] Using worker: sync
[2019-06-11 23:12:48 +0000] [14619] [INFO] Booting worker with pid: 14619
curl http://127.0.0.1:8000/predict (client call server for prediction)
output: "SERVER WORKS"
The problem arises when I try to use Nginx as well.
/etc/systemd/system/app.service
[Unit]
Description=Gunicorn instance to serve app
After=network.target
[Service]
User=root
Group=www-data
WorkingDirectory=/root/server
ExecStart=/usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app
[Install]
WantedBy=multi-user.target
/etc/nginx/sites-available/app
server {
listen 80;
server_name [SERVER_IP_ADDRESS];
location / {
include proxy_params;
proxy_pass http://unix:/root/server/app.sock;
}
}
The status of my systemd looks fine:
systemctl status app
● app.service - Gunicorn instance to serve app
Loaded: loaded (/etc/systemd/system/app.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2019-06-11 23:24:07 UTC; 1s ago
Main PID: 14664 (gunicorn)
Tasks: 2 (limit: 4915)
CGroup: /system.slice/app.service
├─14664 /usr/bin/python /usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app
└─14681 /usr/bin/python /usr/local/bin/gunicorn --bind unix:app.sock -m 007 wsgi:app
systemd[1]: Started Gunicorn instance to serve app.
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Starting gunicorn 19.9.0
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Listening at: unix:app.sock (14664)
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14664] [INFO] Using worker: sync
gunicorn[14664]: [2019-06-11 23:24:07 +0000] [14681] [INFO] Booting worker with pid: 14681
When I make a request to the server, I have trouble connecting:
curl http://[SERVER_IP_ADDRESS]:80/predict
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.14.0 (Ubuntu)</center>
</body>
</html>
EDIT:
I tried removing server_name [SERVER_IP_ADDRESS]; from /etc/nginx/sites-available/app. I now receive 'Welcome to nginx!' at http://SERVER_IP_ADDRESS, and '404 Not Found' at http://SERVER_IP_ADDRESS/predict
FYI, my flask app only has one route, which is '/predict'
It looks like you don't have Port 80 open, so here's a quick iptables command to do so:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT

Nginx: [emerg] socket() failed (24: Too many open files)

I could see lot more thread created for this issue. Tried all the solutions mentioned on those threads. But None of the options worked for me. So I'm creating this new thread.
OS : CentOS 7.2.1511
Nginx : 1.10.0
Error message:
Nov 2 23:21:36 localhost nginx: Starting nginx: nginx: [emerg] socket() x.x.x.x:80 failed (24: Too many open files)
Nov 2 23:21:36 localhost nginx: [FAILED]
Nov 2 23:21:36 localhost systemd: nginx.service: control process exited, code=exited status=1
Nov 2 23:21:36 localhost systemd: Failed to start SYSV: Nginx is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3 proxy server.
Nov 2 23:21:36 localhost systemd: Unit nginx.service entered failed state.
Nov 2 23:21:36 localhost systemd: nginx.service failed.
Ulimit Value:
[root#serv1 nginx]# ulimit -n
16000
[root#serv1 nginx]#
Sysctl.conf:
fs.file-max = 752415
nginx.conf:
worker_rlimit_nofile 30000;
nginx user Ulimit:
[nginx#serv1 ~]$ ulimit -Sn
16000
[nginx#serv1 ~]$ ulimit -Hn
16000
[nginx#serv1 ~]$
Nginx PID max Open files limit:
Max open files 1024 4096 files
Not sure, why the open files limit is not reflected to nginx process. when I checked the pid limit value from proc directory. It shows default value as mentioned above. Values should be 16000 for both soft & hard limits.
Also, I checked the openfiles limit on the server for nginx process. it does not go above 1042.
openfiles :
[root#serv1 ~]# ps -ef | grep nginx
root 4285 1 0 23:12 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/conf/nginx/nginx.conf
nginx 4286 4285 0 23:12 ? 00:00:00 nginx: worker process
root 4308 4290 0 23:12 pts/1 00:00:00 grep --color=auto nginx
[root#serv1 ~]# lsof -p 4285 | wc -l
1042
[root#serv1 ~]#
Someone please help me to fix this problem. Thanks.
Found the problem. its due to nginx.service file was not created as nginx is compiled manually. Once created nginx.service file and added limitonfile value in the file, fixed my problem.
Hope, this may help to people who had hit with this problem.

nginx error log in custom location result in Permission denied (when starting up) on CentOS 7 in Amazon EC2 instance

I am using an AWS EC2 (c3.xlarge) instance with CentOS 7.x and these are my mount points:
[centos#ip-10-0-4-119 ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvda1 8.0G 1.5G 6.6G 18% /
devtmpfs 3.6G 0 3.6G 0% /dev
tmpfs 3.5G 0 3.5G 0% /dev/shm
tmpfs 3.5G 17M 3.5G 1% /run
tmpfs 3.5G 0 3.5G 0% /sys/fs/cgroup
/dev/xvdb 40G 49M 38G 1% /mnt
tmpfs 707M 0 707M 0% /run/user/1000
The root partition / is 8gb (for the OS) and I have a 40gb drive mounted on /mnt for the website files. This is a standard setup.
I logged in as root (using sudo su -) and changed the ownership of /mnt to centos:centos (default account you login into).
in this dir, made another directory to store all the error logs here: /mnt/errors (dir perm is 755).
Nginx was installed the nginx.conf has the following lines:
user centos;
worker_processes 4;
error_log /mnt/errors/nginx-errors.log crit;
pid /var/run/nginx.pid;
When I start the nginx server with this command: systemctl start nginx.service I get the following errors:
[centos#ip-10-0-4-119 ~]$ sudo systemctl status nginx
â— nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2016-11-11 02:20:14 UTC; 7min ago
Docs: http://nginx.org/en/docs/
Process: 10394 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Nov 11 02:20:14 ip-10-0-4-119.localdomain systemd[1]: Starting nginx - high performance web server...
Nov 11 02:20:14 ip-10-0-4-119.localdomain nginx[10394]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 11 02:20:14 ip-10-0-4-119.localdomain nginx[10394]: nginx: [emerg] open() "/mnt/errors/nginx-errors.log" failed (13: Permission denied)
Nov 11 02:20:14 ip-10-0-4-119.localdomain nginx[10394]: nginx: configuration file /etc/nginx/nginx.conf test failed
Nov 11 02:20:14 ip-10-0-4-119.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 11 02:20:14 ip-10-0-4-119.localdomain systemd[1]: Failed to start nginx - high performance web server.
Nov 11 02:20:14 ip-10-0-4-119.localdomain systemd[1]: Unit nginx.service entered failed state.
Nov 11 02:20:14 ip-10-0-4-119.localdomain systemd[1]: nginx.service failed.
Why am I not able to use a custom location to store the nginx errors?
make sure that the directory where logs will go is readable, writable and executable by the nginx user since that'll be the owner of the nginx worker processes. here's what my nginx log directory looks like:
[root#ip-10-0-1-1 opt]# ls -ld /var/log/nginx/
drwx------ 2 nginx nginx 4096 Nov 11 03:27 /var/log/nginx/
if that doesn't make sense, you can do this:
chmod -R 755 /mnt/errors
chown -R nginx:nginx /mnt/errors
HTH

Resources