nginx安裝在上一篇博文。https://www.cnblogs.com/AganRun/p/12951618.html
演示如何用nginx分發前端請求和后端請求。
部署前端
- 首先,隨便找個前端模板,放到服務器的目錄下。我這里放到了nginx安裝目錄下的html文件夾
[root@learn200 html]# pwd
/usr/local/nginx/html
[root@learn200 html]# ll
total 56
-rw-r--r--. 1 root root 537 May 24 16:45 50x.html
-rw-r--r--. 1 root root 6620 May 24 17:57 about.html
-rw-r--r--. 1 root root 12575 May 24 17:57 blog.html
-rw-r--r--. 1 root root 5791 May 24 17:57 contact.html
drwxr-xr-x. 2 root root 104 May 24 17:57 css
drwxr-xr-x. 4 root root 38 May 24 17:57 fonts
drwxr-xr-x. 2 root root 183 May 24 17:57 images
-rw-r--r--. 1 root root 11012 May 24 17:57 index.html
drwxr-xr-x. 2 root root 230 May 24 17:57 js
-rw-r--r--. 1 root root 7608 May 24 17:57 portfolio.html
- 打開瀏覽器,訪問本地80端口,發現首頁可以被正常訪問
部署后端
- 簡單寫兩個flask Demo
from flask import Flask
server=Flask(__name__)
@server.route('/api/test')
def test():
return "Hello AganRun 8081"
server.run(port=8081, host='0.0.0.0')
同樣復制了一個Demo,端口起在了8082,為了訪問區分訪問,我返回了8082
- 啟動服務
[root@learn200 python]# python3 demo1.py
* Serving Flask app "demo1" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://0.0.0.0:8081/ (Press CTRL+C to quit)
訪問
[root@learn200 python]# curl localhost:8081/api/test
Hello AganRun 8081
[root@learn200 python]# curl localhost:8082/api/test
Hello AganRun 8082
NGINX代理
上面的后端訪問是直接通過端口訪問的,現在由nginx代理請求,
把80端口上的/api開頭的請求,負載均衡的代理到flask項目。
- 編輯nginx.conf,設置為如下內容。重點關注upstream
user nobody nobody;
worker_processes 1; #設置值和CPU核心數一致
error_log /usr/local/nginx/logs/nginx_error.log crit; #日志位置和日志級別
pid /usr/local/nginx/logs/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 65535;
}
http
{
include 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';
#charset gb2312;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on; #開啟壓縮
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
upstream mysvr { # flask項目地址
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
#limit_zone crawler $binary_remote_addr 10m;
#下面是server虛擬主機的配置
server
{
listen 80;#監聽端口
server_name localhost;#域名
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$ #靜態資源
{
root /usr/local/nginx/html;
expires 30d;
}
location ~ .*\.(js|css|html)$ #前端文件
{
root /usr/local/nginx/html;
expires 15d;
}
location ~ /api/ # 后端請求
{
proxy_pass http://mysvr;
proxy_redirect default;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
access_log logs/access.log;
}
}
- 重啟
檢驗配置無誤后重啟nginx
[root@learn200 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@learn200 conf]# nginx -s reload
- 請求
可以看到,請求以及被代理,並分發到不同的節點
[root@learn200 conf]# curl localhost/api/test
Hello AganRun 8081[root@learn200 conf]# curl localhost/api/test
Hello AganRun 8082[root@learn200 conf]# curl localhost/api/test
Hello AganRun 8081[root@learn200 conf]# curl localhost/api/test
Hello AganRun 8082[root@learn200 conf]#