Nginx从入门到精通

embedded/2024/9/23 21:21:33/

第一章=>
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即可。

http://www.ppmy.cn/embedded/26106.html

相关文章

cartographer问题处理

问题1 : CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: GMOCK_LIBRARY (ADVANCED)linked by target "time_conversion_test&quo…

Android Studio的settings.gradle配置

pluginManagement { repositories { maven { url ‘https://maven.aliyun.com/nexus/content/groups/public/’} maven { url ‘https://maven.aliyun.com/nexus/content/repositories/gradle-plugin’} maven { url ‘https://maven.aliyun.com/nexus/content/repositories/ap…

Rust中的并发性:Sync 和 Send Traits

在并发的世界中,最常见的并发安全问题就是数据竞争,也就是两个线程同时对一个变量进行读写操作。但当你在 Safe Rust 中写出有数据竞争的代码时,编译器会直接拒绝编译。那么它是靠什么魔法做到的呢? 这就不得不谈 Send 和 Sync 这…

Flutter笔记:Widgets Easier组件库(3)使用按钮组件

Flutter笔记 Widgets Easier组件库(3):使用按钮组件 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite:http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddre…

头歌:Spark案例剖析 - 谷歌网页排名引擎PageRank实战

第1关:海量数据导入:SparkSQL大数据导入处理 任务描述 工欲善其事必先利其器,大数据分析中最重要的是熟练掌握数据导入工具的使用方法。Spark SQL是Spark自带的数据库,本关你将应用Spark SQL的数据导入工具实现文本数据的导入。其中,graphx-wiki-vertices.txt文件中含有网…

Docker数据管理和Dockerfile

目录 一.数据管理 1.作用 (1)修改配置文件例如,nginx.conf /usr/local/nginx/conf/nginx.conf —>/container_nginx/conf/nginx.conf (2)容器内部产生的日志,如何收集将容器内部存方日志文件的目录挂…

C语言【动态内存】

1.为什么要有动态内存 我们现在掌握的内存开辟方法有: int val 20;//在栈空间开辟4个字节 char str[10]{0};//在栈空间开辟10个字节的连续的空间但是上述的方式有两个点要注意: 1.空间开辟的大小是固定的 2.数组在申明的时候,一定要指定数…

Flink checkpoint 源码分析- Checkpoint barrier 传递源码分析

背景 在上一篇的博客里,大致介绍了flink checkpoint中的触发的大体流程,现在介绍一下触发之后下游的算子是如何做snapshot。 上一篇的文章: Flink checkpoint 源码分析- Flink Checkpoint 触发流程分析-CSDN博客 代码分析 1. 在SubtaskCheckpointCoo…