linux nginx 简单负载均衡 和多端口配置

nginx负载均衡配置

## Nginx需要访问linux文件系统,必须有文件系统的权限。User root代表nginx访问文件系统的权限是root用户权限。如果不开启权限,可能有404访问错误
user  root;
worker_processes  1;



events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    ## 负载均衡 服务器配置 weight:服务器访问的权重参数越大访问该服务器的几率越大
    upstream boot{
        server localhost:82 weight=1;
    
    }

    server {
        ## 监听端口
        listen       80;
        server_name  localhost;

        

        location / {
            ## 跳转地址  boot为上面的负载策略
            proxy_pass http://boot;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


    server {
        ## 监听的8833端口
        listen       8833;
        server_name  local;

        location / {
            ## 跳转地址
            proxy_pass http://localhost:82;
            
            proxy_set_header Host $host:8833;
      
            proxy_set_header X-Real-IP $remote_addr;
      
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

}

访问失败,先检查防火墙是否配置端口可以访问

 

同一个端口,不同的路径来访问不同的项目

user  root;
worker_processes  1;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    
    upstream boot{
        server localhost:82;
        server 122.51.40.169:82;
    
    }
    
    upstream boot1{
        server localhost:82;
    
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #其他路径默认访问前台网站
        location / {
            proxy_pass http://boot;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
        
        #通过访问oo二级目录来访问后台
        location /oo/ {
            #localhost:82后面的斜杠是一个关键,没有斜杠的话就会传递service到后端节点导致404
            proxy_pass http://localhost:82/;
            proxy_redirect  off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


}

 

posted @ 2023-07-28 17:17  译林  阅读(313)  评论(0编辑  收藏  举报