Linux笔记(nginx)

详细介绍
一、配置文件的详细介绍

# For more information on configuration, see:
		#   * Official English Documentation: http://nginx.org/en/docs/
		#   * Official Russian Documentation: http://nginx.org/ru/docs/

		user nginx;
		worker_processes auto;  # nginx工作进程数,根据cpu的核数定义
		error_log /var/log/nginx/error.log;  # 
		pid /run/nginx.pid;

		# Load dynamic modules. See /usr/share/nginx/README.dynamic.
		include /usr/share/nginx/modules/*.conf;

		events {
			worker_connections 1024;  # 连接数
		}

		# 定义nginx的核心web功能
		http {
			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  /var/log/nginx/access.log  main;  # 开启访问日志功能的参数

			sendfile            on;
			tcp_nopush          on;
			tcp_nodelay         on;
			keepalive_timeout   65;  # 保持链接连接时长
			types_hash_max_size 2048;  # 

			include             /etc/nginx/mime.types;
			default_type        application/octet-stream;
			gzip on; # 启动压缩模式
			# Load modular configuration files from the /etc/nginx/conf.d directory.
			# See http://nginx.org/en/docs/ngx_core_module.html#include
			# for more information.
			include /etc/nginx/conf.d/*.conf;
			   upstream django {
				# 连接到Django的端口号, 和uwsgi.ini文件中端口一致. 127.0.0.1说明只允许本地转发
				 server 127.0.0.1:8001; # for a web port socket (we'll use this first
				 }
				 
				 
				 
			server {
				listen       80;  # 定义nginx的入口端口是80端口
				server_name  47.100.106.169;  # 服务器域名或ip
				charset     utf-8;  # 定义编码
				access_log      /home/chengjian-env/python_tx晨检/nginx_access.log;  # 自定义日志文件
				error_log       /home/chengjian-env/python_tx晨检/nginx_error.log;
				client_max_body_size 75M;
				location /static {
					alias /home/chengjian-env/python_tx晨检/static; # your Django project's static files - amend as required
				}
				# 用户请求是 47.100.106.169/
				location / { 
					# root html  # 定义网页根目录(定义虚拟主机的根目录)
					uwsgi_pass  django;
					# index index.html  # 定义网页的首页文件的名字
					include     /home/chengjian-env/python_tx晨检/uwsgi_params; # the uwsgi_params file you installed
				}
				# error_page 404 /404.html;  # 配置错误页面
				# error_page 500 502 503 504 /50x.html; 

			}
			
#####################################另一个服务#######################################			
			#  开启一个虚拟nginx主机
			server {
				listen       80;  # 定义nginx的入口端口是80端口
				server_name  www.lilongfei.com;  # 服务器域名或ip
				charset     utf-8;  # 定义编码
				access_log      /home/chengjian-env/python_tx晨检/nginx_access.log;  # 自定义日志文件
				error_log       /home/chengjian-env/python_tx晨检/nginx_error.log;
				client_max_body_size 75M;
				location /static {
					alias /home/chengjian-env/python_tx晨检/static; # your Django project's static files - amend as required
				}
				# 用户请求是 47.100.106.169/
				location / { 
					# deny 192.168.1.1  #  封掉192.168.1.1
					# root html  # 定义网页根目录(定义虚拟主机的根目录)
					uwsgi_pass  django;
					# index index.html  # 定义网页的首页文件的名字
					include     /home/chengjian-env/python_tx晨检/uwsgi_params; # the uwsgi_params file you installed
				}
				# error_page 404 /404.html;  # 配置错误页面
				# error_page 500 502 503 504 /50x.html; 

			}
			
			
			

		# Settings for a TLS enabled server.
		#
		#    server {
		#        listen       443 ssl http2 default_server;
		#        listen       [::]:443 ssl http2 default_server;
		#        server_name  _;
		#        root         /usr/share/nginx/html;
		#
		#        ssl_certificate "/etc/pki/nginx/server.crt";
		#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
		#        ssl_session_cache shared:SSL:1m;
		#        ssl_session_timeout  10m;
		#        ssl_ciphers HIGH:!aNULL:!MD5;
		#        ssl_prefer_server_ciphers on;
		#
		#        # Load configuration files for the default server block.
		#        include /etc/nginx/default.d/*.conf;
		#
		#        location / {
		#        }
		#
		#        error_page 404 /404.html;
		#            location = /40x.html {
		#        }
		#
		#        error_page 500 502 503 504 /50x.html;
		#            location = /50x.html {
		#        }
		#    }

		}

二、nginx的反向代理功能(自带了反向代理的功能,天生的二道贩子)

  1. 实验准备

     准备2个服务器,2个nginx
     nginx1: 192.168.13.14
     nginx2: 192.168.13.19(反向代理服务器)
    
  2. 在反向代理nginx服务器配置文件中修改:

     location / { 
     		proxy_pass http://192.168.13.14;
     	}
    

    nginx.conf

     worker_processes  1;
     error_log  logs/error.log;
     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;
         keepalive_timeout  65;
         server {
             listen       80;
             server_name  192.168.13.19;
             location / {
             proxy_pass http://192.168.13.14;
                 root   html;
                 index  index.html index.htm;
             }
             error_page   500 502 503 504  /50x.html;
             location = /50x.html {
                 root   html;
             }
         }
     }	
    

    此时访问master的服务器192.168.13.19:80地址,已经会将请求转发给slave80端口
    除了页面效果的展示以外,还可以通过log(access.log)查看代理效果

三、nginx负载均衡(集群)

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
Web服务器,直接面向用户,往往要承载大量并发请求,单台服务器难以负荷,我使用多台WEB服务器组成集群,前端使用Nginx负载均衡,将请求分散的打到我们的后端服务器集群中,
实现负载的分发。那么会大大提升系统的吞吐率、请求性能、高容灾

在这里插入图片描述
1、Nginx要实现负载均衡需要用到proxy_pass代理模块配置
2、Nginx负载均衡与Nginx代理不同地方在于Nginx代理仅代理一台服务器,而Nginx负载均衡则是将客户端请求代理转发至一组upstream虚拟服务池
3、Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用

实验
实验一(简单实验):

  1. 实验准备:

     准备三台计算机
     nginx1 192.168.13.121作为nginx负载均衡器
     nginx2 192.168.13.79web服务,提供一个页面  # 因该和下面是同一个页面
     nginx3 192.168.13.24web服务,提供一个页面
    
  2. 作为nginx负载均衡器的配置

     # 定义一个负载均衡池(算法有1.默认轮询2.ip_hash3.weight权重)
     upstream webserver{
     	# ip_hash;  # 不能和weight同时存在(根据ip hash 分配)
     	server 192.168.13.79 weight=10;  # weight代表权重
     	server 192.168.13.24 weight=1;
     }
     # 将用户的请求直接转发给负载均衡池中的服务器
     location / { 
     	proxy_pass http://webserver;
     }
    

    此时初步负载均衡已经完成,upstream默认按照轮训方式负载,每个请求按时间顺序逐一分配到后端节点。
    upstream分配策略:

     调度算法      概述
     轮询        按时间顺序逐一分配到不同的后端服务器(默认)
     weight       加权轮询,weight值越大,分配到的访问几率越高
     ip_hash      每个请求按访问IP的hash结果分配,这样来自同一IP的固定访问一个后端服务器
     url_hash      按照访问URL的hash结果来分配请求,是每个URL定向到同一个后端服务器
     least_conn    最少链接数,那个机器链接数少就分发
    

    提示:

    1. 轮询(不做配置,默认轮询)

    2. weight权重(优先级)

       upstream django {
              server 10.0.0.10:8000 weight=5;
              server 10.0.0.11:9000 weight=10;#这个节点访问比率是大于8000的
       }
      
    3. ip_hash配置,根据客户端ip哈希分配,不能和weight一起用weight 权重

       # 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器
       upstream django {
           ip_hash;
              server 10.0.0.10:8000;
              server 10.0.0.11:9000;
       }
      
    4. backup(在非backup机器繁忙或者宕机时,请求backup机器,因此机器默认压力最小)

       upstream django {
              server 10.0.0.10:8000 weight=5;
              server 10.0.0.11:9000;
              server node.oldboy.com:8080 backup;
       }
      

实验二(负载均衡):

  1. 角色:

     角色            ip                           主机名
     lb01        192.168.119.10            lb01    
     web01        192.168.119.11        web01
     web02        192.168.119.12        web02
    
  2. 关闭防火墙

     iptables -F
     sed  -i 's/enforcing/disabled/' /etc/selinux/config
     systemctl stop firewalld
     systemctl disable firewalld
    
  3. web01web02服务器配置nginx,创建index.html

    web01

     	server {
     	        listen       80;
     	        server_name  192.168.119.11;
     	        location / {
     	        root /node;
     	            index  index.html index.htm;
     	        }
     	}
    
    
     mkdir /node
     echo 'i am web01' > /node/index.html
     #启动NGINX
     	./sbgin/nginx
    

    web02

     server {
         listen       80;
         server_name  192.168.119.12;
         location / {
             root /node;
             index  index.html index.htm;
     }
    
    
     mkdir /node
     echo 'i am web02...' > /node/index.html
     #启动nginx
     	./sbing/nginx
    
  4. 配置lb01服务器的nginx负载均衡

    1. 手动创建proxy_params文件,文件中存放代理的请求头相关参数

       [root@lb01 conf]# cat /opt/nginx/conf/proxy_params
       [proxy_params]
       proxy_set_header Host $http_host;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_connect_timeout 30;
       proxy_send_timeout 60;
       proxy_read_timeout 60;
       proxy_buffering on;
       proxy_buffer_size 32k;
       proxy_buffers 4 128k;
      
    2. 配置lb01的 nginx.conf

       http {
           include       mime.types;
           default_type  application/octet-stream;
           sendfile        on;
           keepalive_timeout  65;
           upstream node {
             server 192.168.119.11:80;
             server 192.168.119.12:80;
       }
           server {
               listen       80;
               server_name 192.168.119.10;
               location / {
                 proxy_pass http://node;
                 include proxy_params;  #需要手动创建
               }
           }
       }
      
    3. 启动

      启动lb01负载均衡nginx服务

       ./sbin/nginx
      
  5. 访问lb01节点nginx,反复刷新

    在这里插入图片描述

    在这里插入图片描述

实验三(nginx动静分离):

在这里插入图片描述

  1. 角色:

     系统              服务       软件             ip地址
     centos7(lb01)   负载均衡   nginx proxy    192.168.119.10
     centos7(web01)  静态资源   nginx静态资源   192.168.119.11
     centos7(web02)  动态资源   django         92.168.119.12
    
  2. web01web02机器上,配置静态资源,图片等

    web01

     	cat nginx.conf
     	server {
     	        listen       80;
     	        server_name  192.168.119.11;
     	        #定义网页根目录
     	         root /code;
     	        #定义了静态资源
     	        index index.html;
     	#域名匹配,所有的png、jpg、gif请求资源,都去/root/code/images底下找
     	         location ~* .*\.(png|jpg|gif)$ {
     	                root /code/images;
     	        }    
    
     	#重启nginx
     		./sbin/nginx
    

    #创建目录

     	mkdir -p /code/images
    

    #准备首页文件

     	[root@web01  /code]$cat index.html
     	static files...
    

    #准备静态文件,图片

     	[root@web01  /code/images]$wget http://pythonav.cn/av/girlone.jpg^C
     	[root@web01  /code/images]$ls
     	girlone.jpg
    

    web02

     cat  nginx.conf
     #静态资源地址
     upstream static {
     server 192.168.119.11:80;
     }
     #flask动态请求
     upstream flask {
     server 192.168.119.12:8080;
     }
         server {
             listen       80;
             server_name  192.168.119.12;
           #当请求到达192.168.119.12:80/时,转发给flask的8080应用
             location / {
                 proxy_pass http://flask;
                 include proxy_params;
             }
           #当判断资源请求是 192.168.119.12/girl.jpg时候,转发请求给static地址池的服务器192.168.119.11/
             location ~ .*\.(png|jpg|gif)$ {
             proxy_pass http://static;
     include proxy_params;
     }
    

    准备flask应用,flask.py

     from flask import Flask
     app=Flask(__name__)
     @app.route('/')
     def hello():
         return "i am flask....from nginx"
     if __name__=="__main__":
         app.run(host='0.0.0.0',port=808
    

    后台运行flask程序

     python flask-web.py &
    
  3. 在负载均衡服务器lb01上测试访问192.168.119.10

    在这里插入图片描述

    在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值