Ngnix静态代理和请求转发配置

结构

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值