I looked it up in 2014 and personally said this is the Python API server settings.
Put nginx on the front and use gunicorn on the application server to reverse proxy.
It's a bit old, but I decided on this by referring to Instagram. INSTAGRAM ENGINEERING
A memo when setting to AWS EC2.
Installed using pip on virtualenv. I use virtualenv because I don't like yum getting stuck when I play with Python on the OS, and I want to raise the Python version.
I'm not very familiar with it, but I registered for upstart.
Describe the following configuration file.
/etc/init/myapp.conf
description "[APP NAME] API Server"
start on runlevel [2345]
stop on runlevel [016]
respawn
script
su - www
/home/www/[APP NAME]/virtualenv/bin/gunicorn -b 127.0.0.1:8000 -w 4 -u www --chdir /home/www/[APP NAME]/server/python --log-file /home/www/[APP NAME]/logs/error_log --pythonpath /home/www/[APP NAME]/server/python server:application
end script
At the time of centos7, upstart is not included, so set it to systemctl. You can find it on the Gunicorn site, including the configuration file.
Reference: Deploying Gunicorn --Gunicorn 19.3.0 documentation
Install with yum.
nginx configuration file.
/etc/nginx/nginx.conf
user www;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
index index.html;
upstream app_server {
server 127.0.0.1:8000 fail_timeout=0;
}
server {
listen 80;
server_name [DOMAIN];
root /home/www/[APP NAME]/server/html;
access_log /var/log/nginx/[APP NAME].access.log main;
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
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;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
The main settings are the following two.
Recommended Posts