Nginx从入门到精通

第一章=>
1、概述                     
2、正反向代理                 
3、负载均衡             
4、Nginx安装        

第二章=>
5、常用命令               
6、实战总结                     
7、前端部署               

****************************************************************************************************************************************************************************

1、Nginx概述
【1】当访问量变大,服务器承受不住了,服务器就需要多台
【2】多台服务器的session怎么处理与统一呢?
【3】迫切需要中间件包一层。总的服务器(Nginx)起到代理的作用!!!
【4】代理服务器访问,叫做反向代理。
【5】但是当服务器的内存不一样,64G多被访问,8G少点,就需要用到Nginx的负载均衡
【6】没有什么是加一层解决不了的,如果有就再加一层!!!!!!!!!!!
【7】Nginx是一个高性能的HTTP   反向代理web服务器。
占用内存小1M,并发能力强5万个并发连接数的响应,卧槽 牛批呀!!!!!!!!

****************************************************************************************************************************************************************************

2、正反向代理   
【1】访问国外服务器特别慢,谷歌下载东西特别慢
连接VPN、翻墙软件,先从访问代理软件香港---香港访问国外服务器,然后再返回。
这就叫做"正向代理"  代理的是客户端!!!!!!!!!!!!
客户端1/2/3---VPN-----云----服务器
**************************************************************************************
反向代理是代理服务器!!!!!!!!!!!!!!
客户端---云---Nginx---服务器1/2/3

****************************************************************************************************************************************************************************

3、负载均衡
【1】轮询 :轮流来
【2】加权轮询:权重越高,访问的可能性越大
【3】ip hash: 就是根据ip去保存,但是也是有风险。所以最终是根据Reids做的session共享
【4】动静分离:静态资源从Nginx返回,感觉用处不大。

****************************************************************************************************************************************************************************

4、Nginx安装
【1】Windows下面使用
解压就可以使用,C:\IT\Nginx\nginx-1.22.1
**********************************************************************************************
listen       80;
**********************************************************************************************
cd C:\IT\Nginx\nginx-1.22.1
nginx.exe启动
访问localhost即可
【2】Linux下面的安装使用
前置准备:
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
**********************************************************************************************
安装:
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
**********************************************************************************************
执行:
cd /opt/nginx/nginx-1.13.7
./configure
执行make命令(要是执行不成功请检查最开始安装的四个有没有安装成功)  
报错执行vim src/os/unix/ngx_user.c  注释掉=~salt[0];哪行
再报错:vim objs/Makefile 删除里面的-Werror
make
make install
whereis nginx  看看在什么位置
**********************************************************************************************
启动:
进入/usr/local/nginx/sbin目录
输入./nginx
./nginx -s quit  或者 ./nginx -s stop
./nginx -s reload
访问云服务器IP,既可以看到Nginx欢迎页面
**********************************************************************************************
设置开机自启动
vim /etc/rc.local
//最底部增加这一行
/usr/local/nginx/sbin/nginx
**********************************************************************************************
ps -ef|grep nginx查看进程,用来kill -9 停止

****************************************************************************************************************************************************************************

5、常用命令
【1】Linux编辑配置文件是在
vim /usr/local/nginx/conf/nginx.conf
cd /usr/local/nginx/sbin/nginx
./nginx -s reload
【2】解决一个报错问题
nginx: [emerg] unknown directive "" in /usr/local/nginx/conf/nginx.conf:3
使用notepad++编辑保存成UTF-8格式的即可解决问题。亲测可行!!!!!
【3】可以根据文件修改相关配置
vim /usr/local/nginx/conf/nginx.conf
***********************************************************************************************
# 完整配置备份
#user  nobody;
user root;
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       8001;
        server_name  localhost;
        # compression-webpack-plugin 配置
        gzip on;
        gzip_min_length 1k;
        gzip_comp_level 9;
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
       gzip_vary on;
        # 配置禁用 gzip 条件,支持正则,此处表示 ie6 及以下不启用 gzip(因为ie低版本不支持)
        gzip_disable "MSIE [1-6]\."; 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
            #root   html;
            #index  index.html index.htm;
        #}
        location / {
              root /root/site/white/dist;
              index index.html index.htm;
              try_files $uri $uri/ /index.html;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}

****************************************************************************************************************************************************************************

6、实战总结
【1】以Linux为例看下负载均衡
按照4的步骤,一顿操作,成功http://192.168.0.105/   显示出Welcome to nginx!
【2】在Linux上启动两个端口的服务
经过自己配置JDK、写简单的能拿到port的服务(spring initializer)可以作为模板使用 
http://192.168.0.105/   返回Nginx的欢迎页面
http://192.168.0.105:9001/port   返回9001
http://192.168.0.105:9002/port   返回9002
****************************************************************************************
vim /usr/local/nginx/conf/nginx.conf配置文件梳理说明
全局配置
events{
worker_connections  1024;  // 连接数
}
http{  配置
   upstream xx{
    // 负载均衡配置
   }
   server {
		listen       80;
		server_name  localhost;
		// 代理
		location / {
			root   html;
			index  index.html index.htm;
		}
	}
	server {
		listen       443;
		server_name  localhost;
		// 代理
	}
}
****************************************************************************************
# 配置负载均衡
 35     upstream wdfgdzx{
 36        # 服务器资源
 37        server 192.168.0.105:9001 weight=1;   // 注意这里的服务器地址可以随便指定
 38        server 192.168.0.105:9002 weight=2;   // 就说明可以带来不同的机器!!!!!
 39     }
****************************************************************************************
# 配置反向代理
server {
 43         listen       80;
 44         server_name  localhost;
 45         
 46         #charset koi8-r;
 47         
 48         #access_log  logs/host.access.log  main;
 49         
 50         location / {
 51             root   html;
 52             index  index.html index.htm;
 53             proxy_pass http://wdfgdzx;  !!!!!!!!!wdfgdzx和上面upstream名字是一个名字
 54         }  
 ****************************************************************************************
 使得最新配置生效
cd /usr/local/nginx/sbin
./nginx -s reload
****************************************************************************************
访问http://192.168.0.105/port
会不断的展示9001  或者 9002(而且他会展示两次,这就是负载均衡的权重起作用了,牛批了!!!!)
server 192.168.0.105:9001 weight=1;   // 注意这里的服务器地址可以随便指定
server 192.168.0.105:9002 weight=2;   // 就说明可以带来不同的机器!!!!!
****************************************************************************************
这个比例很好玩呀,实际应用是64G机器访问8次,则8G的只能访问1次,则配置:
server 192.168.0.105:9001 weight=8;   
server 192.168.0.105:9002 weight=1;   
【3】来更高级点,我在阿里云上配置一个,顺便验证下是否支持域名....
http://wdfgdzx.top:8080/port  返回8080端口
配置
vim /usr/local/nginx/conf/nginx.conf
使得最新配置生效
cd /usr/local/nginx/sbin
./nginx -s reload
# 配置负载均衡
 35     upstream wdfgdzx{
 36        # 服务器资源
 37        server 192.168.0.105:9001 weight=1;   // 注意这里的服务器地址可以随便指定
 38        server 192.168.0.105:9002 weight=2;   // 就说明可以带来不同的机器!!!!!
              server wdfgdzx.top:8080 weight=3;  
 39     }
说明确实支持公网IP,其次支持域名的影射配置,好屌呀!!!!!!!!!!!!!!!
****************************************************************************************
就真的可以根据自己机器的配置,来配置权重了!!!!!!!!!!!!! 
【4】别的功能
@动静分离
@重写: 80 http rewarite ---> https 443
@静态资源可以直接用nginx代理,不用去请求tomcat,这不就是前后端分离...
@只需要后端多跑几台机器就OK了... 我扔....

****************************************************************************************************************************************************************************

7、前端服务器部署
【1】基础工作
npm run build打包出来dist
npm install anywhere -g 安装前端静态服务器插件
把dist上传到自己的服务器,配置nginx反向代理vue
【2】配置工作
vim /usr/local/nginx/conf/nginx.conf
配置如下
location / {
              root /root/site/UserInterface/dist;
              index index.html index.htm;
              try_files $uri $uri/ /index.html;
}
开头加:
user root;
【3】切换目录,重启nginx
cd /usr/local/nginx/sbin
./nginx -s reload 
【4】后续Nginx机器配置,查看CENTOS版本,cat /etc/redhat-release即可。
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值