Nginx部署多个Flask项目

写一下这篇文章,主要是记录下Nginx部署的要点,防止自己以后忘记。

部署多个flask项目的时候其实要注意的就是要在nginx的配置里面同时启动两个server,且对应不同的端口,注意server name也不能重复可以用二级域名代替,如果有强迫症的话。具体配置如下。


upstream mycluster{
        server 127.0.0.1:8080;
}       
      server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
    #SSL 访问端口号为 443
    listen 443 ssl; 
    #填写绑定证书的域名
    server_name chereby.com; 
    #证书文件名称
    ssl_certificate 1_chereby.com_bundle.crt; 
    #私钥文件名称
    ssl_certificate_key 2_chereby.com.key; 
    ssl_session_timeout 5m;
    #请按照以下协议配置
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    #请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE; 
    ssl_prefer_server_ciphers on;
    location / {    #注意用的端口号
	    proxy_pass http://127.0.0.1:8080;
	    proxy_redirect off;
	    proxy_set_header Host $http_host;
	    proxy_set_header X-Real-IP $remote_addr;
	    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	    proxy_set_header X-Fprwarded-Proto $scheme;
}    
     error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }}
    server {
        listen       80;
        root         /usr/share/nginx/html;
        include /etc/nginx/default.d/*.conf;    #二级域名,要先解析后再放进来
        server_name cov.chereby.com; 
        location / {    #第二个服务的端口号
	proxy_pass http://127.0.0.1:5000;
	proxy_redirect off;
	proxy_set_header Host $http_host;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	proxy_set_header X-Fprwarded-Proto $scheme;
}
     error_page 404 /404.html;
        location = /404.html {
        }
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }
}

欢迎技术探讨