Nginx配置代理
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]#