结构
http{
upstream 服务名{}
server{
listen 8001;
location /请求路径 {
proxy:
}
}
}
代码示例
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream nac {
server 139.224.234.236:8848 weight=1;
}
server {
listen 8001;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /;
}
location /images/ {
root /home;
}
location /nacos {
proxy_connect_timeout 10;
proxy_read_timeout 24h;
proxy_pass http://nac;
proxy_set_header Host $host:$server_port;
proxy_set_header Accept-Encoding "";
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Accept-Language "zh-CN";
proxy_http_version 1.1;
proxy_buffering off;
}
静态资源服务器
ngnix 就相当于一个静态资源服务器
直接访问ip:端口 服务器路径访问
http://139.224.234.236:8001/home/images/1.jpg
【配置】
server 内配置静态资源
location / {
root /; #请求根目录路径 localhost:/
}
location /images/ {
root /home; #实际请求localhost:/images 转发到localhost:/home/images/
}
负载均衡请求转发
1、配置上游服务地址ip pro_name代表服务名称
upstream pro_name {
server 139.224.234.236:8848 weight=1;
}
2、配置代理请求转发路径、nacos为ngnix的转发路径url
location /nacos {
proxy_connect_timeout 10;
proxy_read_timeout 24h;
proxy_pass http://nac;
proxy_set_header Host
h
o
s
t
:
host:
host:server_port;
proxy_set_header Accept-Encoding “”;
proxy_set_header User-Agent $http_user_agent;
proxy_set_header Accept-Language “zh-CN”;
proxy_http_version 1.1;
proxy_buffering off;
}