Nginx入门

Nginx的主要作用

  • 反向代理
  • 负载均衡

 

 

 

什么是Nginx?

正向代理和方向代理的区别?

虽然正向代理服务器和反向代理服务器所处的位置都是客户端和真实服务器之间,所做的事情也都是把客户端的请求转发给服务器,再把服务器的响应转发给客户端,但是二者之间还是有一定的差异的。

1、正向代理其实是客户端的代理,帮助客户端访问其无法访问的服务器资源。反向代理则是服务器的代理,帮助服务器做负载均衡,安全防护等。

2、正向代理一般是客户端架设的,比如在自己的机器上安装一个代理软件。而反向代理一般是服务器架设的,比如在自己的机器集群中部署一个反向代理服务器。

3、正向代理中,服务器不知道真正的客户端到底是谁,以为访问自己的就是真实的客户端。而在反向代理中,客户端不知道真正的服务器是谁,以为自己访问的就是真实的服务器。

4、正向代理和反向代理的作用和目的不同。正向代理主要是用来解决访问限制问题。而反向代理则是提供负载均衡、安全防护等作用。二者均能提高访问速度。

 

Nginx服务器负载均衡策略

nginx的upstream目前支持的5种方式的分配(只介绍前三种):

 

  • 轮询(默认)
    •   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 
      upstream backserver { 
      server 192.168.0.14; 
      server 192.168.0.15; 
      }
  • 权重
    •   指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 
      upstream backserver { 
      server 192.168.0.14 weight=8; 
      server 192.168.0.15 weight=10; 
      }
  • iphash
    • 每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 
      upstream backserver { 
      ip_hash; 
      server 192.168.0.14:88; 
      server 192.168.0.15:80; 
      }
相关文章链接:
 

 

什么是动静分离?

动静分离详细介绍链接

 

 


 Nginx安装

nginx中文网站

Windows安装

1.解压压缩包,进入压缩目录,进入conf目录

 

 

 

 2.分析nginx.conf文件

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 #############上述配置为全局配置
 11 events {
 12     worker_connections  1024;#最大连接数为1024
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 ######上述代码为http的全局配置

  ######upstream 负载均衡配合 start
      upstream upStream_1{#upstream名字任意取
      #服务器资源
      server 127.0.0.1:8080 weight=1;#可以更改权重来划分访问流量
      server 127.0.0.1:8081 weight=1;
  }
  #####upstream 负载均衡配置 end
34 server { 35 listen 80; 36 server_name localhost; 37 38 #charset koi8-r; 39 40 #access_log logs/host.access.log main; 41 42 location / { 43 root html; 44 index index.html index.htm;
          proxy_pass http;//upStream_1;#指定方向代理服务器
45 } 46 47 #error_page 404 /404.html; 48 49 # redirect server error pages to the static page /50x.html 50 # 51 error_page 500 502 503 504 /50x.html; 52 location = /50x.html { 53 root html; 54 } 55 56 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 57 # 58 #location ~ \.php$ { 59 # proxy_pass http://127.0.0.1; 60 #} 61 62 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 63 # 64 #location ~ \.php$ { 65 # root html; 66 # fastcgi_pass 127.0.0.1:9000; 67 # fastcgi_index index.php; 68 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 69 # include fastcgi_params; 70 #} 71 72 # deny access to .htaccess files, if Apache's document root 73 # concurs with nginx's one 74 # 75 #location ~ /\.ht { 76 # deny all; 77 #} 78 } 79 80 81 # another virtual host using mix of IP-, name-, and port-based configuration 82 # 83 #server { 84 # listen 8000; 85 # listen somename:8080; 86 # server_name somename alias another.alias; 87 88 # location / { 89 # root html; 90 # index index.html index.htm; 91 # } 92 #} 93 94 95 # HTTPS server 96 # 97 #server { 98 # listen 443 ssl; 99 # server_name localhost; 100 101 # ssl_certificate cert.pem; 102 # ssl_certificate_key cert.key; 103 104 # ssl_session_cache shared:SSL:1m; 105 # ssl_session_timeout 5m; 106 107 # ssl_ciphers HIGH:!aNULL:!MD5; 108 # ssl_prefer_server_ciphers on; 109 110 # location / { 111 # root html; 112 # index index.html index.htm; 113 # } 114 #} 115 116 }

3.在cmd中输入nginx.exe启动nginx服务,并且访问localhost:80端口来访问nginx

 

 

 

 Linux安装

1.tar -zxvf 压缩包

2.进入压缩包 ./configure make&&make install

在这里可能会需要安装相关依赖

编译如果提示./configure: error: the HTTP rewrite module requires the PCRE library.,则执行yum -y install pcre-devel后重新编译
提示./configure: error: the HTTP gzip module requires the zlib library.,则执行yum install -y zlib-devel后重新编译

3.安装成功后 进入 /usr/local/nginx/sbin目录,运行./nginx 启动nginx服务

 

 4.访问Linux服务器的80端口

 

 Nginx常用命令

  • 启动Nginx命令 ./nginx
  • 关闭Nginx命令 ./nginx -s stop
  • 重新记载配置文件 ./nginx -s reload

 

posted @ 2022-05-05 20:49  王广元  阅读(28)  评论(0编辑  收藏  举报
分享到: