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;
		}
 
	}
	
}
相关推荐
Forever Nore9 小时前
LeetCode 14 最长公共前缀 - 纵向扫描
linux·服务器·leetcode
骇客野人9 小时前
基于Nginx+Eureka+Apollo+Jenkins+SpringBoot Web系统分布式部署方案
nginx·eureka·jenkins
小新讲网安10 小时前
HTTP请求走私攻击实战:CL.TE与TE.CL绕过前端服务器全解析
服务器·前端·网络·web安全·http·架构·漏洞
我是谁??21 小时前
Ubuntu22.04更换清华源
linux·运维·服务器
圣殿骑士-Khtangc21 小时前
Go字符串高效拼接性能对比与底层原理分析
服务器·前端·golang
Kendra9191 天前
从 0 到上线:单文件网页 App 部署到阿里云 ECS 全流程(Nginx+Node+PM2+PostgreSQL)
nginx·阿里云·云计算·部署·个人开发·今天吃什么
这个DBA有点耶1 天前
数据库一体机架构演进:从硬件堆叠到软硬深度耦合
服务器·网络·数据库·硬件架构·运维开发·database·数据库架构
测试运维日常笔记1 天前
Linux系统安装配置TigerVNC服务器完整指南
linux·运维·服务器
像风一样自由20201 天前
加强版VScode结合remote-ssh远程连接服务器开发指南
服务器·vscode·ssh
雾时之林1 天前
Linux----cron定时服务
linux·运维·服务器