Nginx的反向代理、动静分离、负载均衡

反向代理

反向代理是一种常见的网络技术,它可以将客户端的请求转发到服务器群集中的一个或多个后端服务器上进行处理,并将响应结果返回给客户端。反向代理技术通常用于提高网站的可伸缩性和可用性,并且可以隐藏真实的后端服务器地址。

复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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;
	

         server {
	 listen 80;
	 server_name localhost;
	 #默认编码
	 charset utf-8;
	 # access_log logs/host.access.log  main;
	 location / {
		 #反向代理的url
		 proxy_pass http://www.baidu.com/;

		}
 
	}
	
}

访问localhost:80

跳转到百度

负载均衡

负载均衡是一种常见的网络技术,用于将流量在多个服务器之间平均分配,以提高系统的性能、可伸缩性和可靠性。负载均衡器(Load Balancer)作为中间层,接收来自客户端的请求,并将请求转发到后端的多个服务器上进行处理。

复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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 httpd {
		server 192.168.2.1:81 weight=10 down;
		server 192.168.2.3:80 weight=1;
		server 192.168.2.4:80 weight=1 backup;
	}

         server {
	 listen 80;
	 server_name localhost;
	 #默认编码
	 charset utf-8;
	 # access_log logs/host.access.log  main;
	 location / {
		 #反向代理的url
		 proxy_pass http://httpd;

		}
 
	}
	  server {
	 listen 81;
	 server_name localhost;
	 #默认编码
	 charset utf-8;
	 # access_log logs/host.access.log  main;
	 location / {
		   root   html;
		   #定义首页文件的名字
		   index index.html index.htm;
		}
 
	}
	
}
  1. down:表示当前的server暂时不参与负载
  2. weight:默认为1.weight越大,负载的权重就越大。
  3. backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。

动静分离

动静分离方面有着广泛应用。使用Nginx进行动静分离可以提高网站的性能和可伸缩性,并减轻后端服务器的压力。

复制代码
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

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 httpd {
		server 192.168.2.1:81 weight=10 down;
		server 192.168.2.3:80 weight=1;
		server 192.168.2.4:80 weight=1 backup;
	}

         server {
	 listen 80;
	 server_name localhost;
	 #默认编码
	 charset utf-8;
	 # access_log logs/host.access.log  main;
	 location / {
		 #反向代理的url
		 proxy_pass http://httpd;

		}
		# 静态资源
		location /css {
		root /usr/local/nginx/static;
		index index.html index.htm;
		}
		location /images {
		root /usr/local/nginx/static;
		index index.html index.htm;
		}
		location /js {
		root /usr/local/nginx/static;
		index index.html index.htm;
		}	
 
	}
	  server {
	 listen 81;
	 server_name localhost;
	 #默认编码
	 charset utf-8;
	 # access_log logs/host.access.log  main;
	 location / {
		   root   html;
		   #定义首页文件的名字
		   index index.html index.htm;
		}
 
	}
	
}
相关推荐
Orlando cron2 分钟前
源哈希(sh)解析
负载均衡·哈希算法
NetX行者2 分钟前
FastMCP:用于构建MCP服务器的开源Python框架
服务器·python·开源
网硕互联的小客服2 小时前
未来趋势:AI与量子计算对服务器安全的影响
运维·服务器·网络·网络安全·量子计算
黑客老李2 小时前
EDUSRC:智慧校园通用漏洞挖掘(涉校园解决方案商)
服务器·前端·网络·安全·web安全
宇钶宇夕2 小时前
STEP 7 MicroWIN SMART V2.2 的详细安装步骤及注意事项
运维·服务器·程序人生·自动化
czhc11400756633 小时前
Linux 77 FTP
linux·运维·服务器
張萠飛4 小时前
Linux中程序的limits中的Max open files的配置由哪些参数决定
linux·运维·服务器
DuelCode13 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
爬山算法15 小时前
MySQL(116)如何监控负载均衡状态?
数据库·mysql·负载均衡
snoopyfly~16 小时前
Ubuntu 24.04 LTS 服务器配置:安装 JDK、Nginx、Redis。
java·服务器·ubuntu