nginx-访问

website --》网站

site --》站点

​ nginx是搭建网站的一个软件

如何卸载编译安装的nginx:删除安装指定的目录 --》–prefix指定的路径

​ 在PATH变量里删除nginx的安装路径

升级问题:重新编译安装一个新的

关机:nginx -s stop 快速的关闭(不管连接状态完成情况,都会关闭)

​ nginx -s quit 推荐 (优雅的关闭。没有访问完的继续访问,新的请求不接受)

​ nginx -s reload (旧的请求不关闭,开启新的请求,新的连接连新的请求,旧的连接处理完成后就将旧的进程关闭)
​ Once the master process receives the signal to reload configuration, it checks the syntax validity of the new configuration file and tries to apply the configuration provided in it. If this is a success, the master process starts new worker processes and sends messages to old worker processes, requesting them to shut down. Otherwise, the master process rolls back the changes and continues to work with the old configuration. Old worker processes, receiving a command to shut down, stop accepting new connections and continue to service current requests until all such requests are serviced. After that, the old worker processes exit.

[root@nginx-kafka01 scpenglinxi99]# nginx -t  测试nginx.conf配置文件是否有错误
nginx: the configuration file /usr/local/scpenglinxi99/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/scpenglinxi99/conf/nginx.conf test is successful

nginx和浏览器之间使用什么协议传输网页?
http
https

动态网页:html–》js–》python 、java、go、php等–》database
静态网页:html:超文本标记语言 编写网页的,达到好看的效果的 --》写死了的页面,不会到数据库里获取数据

是否与后端的数据库进行交互?

​ 前后端分离:
​ 前端就是展示页面内容
​ 后端专门和数据库进行交互(读写数据)

动态语言(python )和静态语言(java):类型需不需要申明
强类型和弱类型 :类型之间可不可以自动转换

为什么工作进程的数量建议和cpu的数量保持一致?

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZplZ70Hd-1644419071125)(C:\Users\xixi\AppData\Roaming\Typora\typora-user-images\image-20220127171850505.png)]

假设电脑上有4个CPU,worker_process的数量设置为2,当有4个工作进程在就绪状态时,如果只设置了一个cpu,那么只能有一个工作进程工作,其他三个就在就绪状态排队,如果有四个核心的话,就能4个工作进程同时run,达到并发的效果。但是如果设置了8个工作进程,但核心只有4个,总是有4个工作进程在就绪队列里排队

一键安装部署nginx脚本里的这行的意思:id xixi || useradd xixi -s /sbin/nologin

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZUQV9Vpt-1644419071126)(C:\Users\xixi\AppData\Roaming\Typora\typora-user-images\image-20220127173849163.png)]

判断有没有xixi这个用户,没有的话就创建,-s /sbin/nologin 表示这个用户不能进行登录,后面(–user=xixi)表示xixi这个用户我们只用来启动nginx进程,不想用来登录系统(登录就是指可以通过ssh远程登录),防止威胁系统安全,如果没有指定启动nginx进程的用户xixi,那么默认就会使用nobody这个用户启动nginx。

日志级别

0 EMERG. emergency 紧急 --》系统不能使用了
1 ALERT. 告警
2 CRIT. 严重 Critical
3 ERR. 程序因为某个错误导致不能运行,只要是error以上的级别都会导致程序启动或者是运行失败
4 WARNING. 警告 ,不会影响程序的正常运行,只是提醒而已
5 NOTICE. 正常的记录,有点影响力的事件
6 INFO. 普通的信息都记录
7 DEBUG. 调试模式,什么都记录

CPU和内存的配比原则:

1个核心配4G的内存

location 路由–>定位

/usr/local/sclilin99/html 存放网页的目录
www.sc.com/fengdeyong —》/usr/local/sclilin99/html/fengdeyong
www.sc.com/hejin —》/usr/local/sclilin99/html/hejin
www.sc.com —>/usr/local/sclilin99/html

URL 统一资源定位符

[root@sc-nginx conf]# cat nginx.conf
#user  nobody;   默认使用nobody用户去启动nginx
worker_processes  2;  工作进程的数量,建议工作进程的数量和cpu核心一致

#error_log  logs/error.log;  错误日志
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;  nginx master的进程号pid


events {
    worker_connections  1024;  并发数量,同时可以允许多少人同时访问nginx --》同时1024个人访问
}
#需要根据实际的cpu,内存,带宽,磁盘IO能力来进行压力测试
#1个cpu核心--》4G的内存

#worker_processes * worker_connections = 2 *1024  = 2048

  
http {   --》http协议的相关的配置
    include       mime.types;
    default_type  application/octet-stream;
     
    #日志格式   给日志格式起名为main
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';       
                         # '$客户端的ip地址 $ 客户端登录的用户 [$时间]  "请求的网址" '
                         # '$状态码  $发送了多少字节数据  "$从哪个网站过来"  '
                         # ' "$用户使用的浏览器"  "$是否通过别的代理中转过来" '
    #  $+变量名:引用nginx内部的变量值,很多都是来自http请求报文和响应报文的字段,nginx可以读懂计算机网络的数据,写到日志文件里去
  
    #access_log  logs/access.log  main; 
    #访问日志   放在logs下命名为access.log    main代表日志的格式

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;   65秒后nginx会主动断开连接

    #gzip  on;  启用压缩功能 --》加快传输的速度的

    server {  --》提供web服务的配置 --》虚拟主机--》网站
        listen       80;   监听80端口
        server_name  www.sc.com;   网站服务的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;  访问日志的路径和格式  

        location / {  --》提供某个路由的配置 --》/  访问网站的根目录
            root   html;   html是存放网页的根目录 --》/usr/local/sclilin99/html
            index  index.html index.htm;  指定首页
        }
           
        location /hejin {
            index  he.html ;
        }

        location /feng {
          proxy_pass   https://www.sina.com;     #proxy_pass:代理服务器,帮我们转发到新浪的页面
        }


        #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;
        }

ulimit限制

出现nginx: [warn] 2048 worker_connections exceed open file resource limit: 1024的警告的原因?

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uhzbDTr0-1644419071128)(C:\Users\xixi\AppData\Roaming\Typora\typora-user-images\image-20220129163200326.png)]

[root@nginx-kafka01 conf]# nginx -t
nginx: the configuration file /usr/local/scpenglinxi99/conf/nginx.conf syntax is ok
nginx: [warn] 2048 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/scpenglinxi99/conf/nginx.conf test is successful
[root@nginx-kafka01 conf]# 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lzatympx-1644419071128)(C:\Users\xixi\AppData\Roaming\Typora\typora-user-images\image-20220129160526121.png)]

原因:

在上面代码中woker_processes的值为2,woker_connection的值为2048,那么一个woker就可以有2048个人访问,2个worker就有两个进程,1个进程就要开启2048个线程,每一个客户访问进来,nginx就开启一个线程来接待,建立连接,一个线程访问进来就要读去我们的网页文件,html文件里拿网页,比如说都访问首页的话就都要读取index.html文件,linux内核里规定了我们一个进程只能打开1024个文件(使用ulimit -a查看),那么nginx的性能就受到了制约。

解决办法:调整内核对一个进程打开文件数量的限制

临时调整:

[root@nginx-kafka01 conf]# ulimit -n 1000000
[root@nginx-kafka01 conf]# ulimit -a
core file size          (blocks, -c) unlimited
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 7061
max locked memory       (kbytes, -l) 64
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1000000
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 8192
cpu time               (seconds, -t) unlimited
max user processes              (-u) 7061
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited
[root@nginx-kafka01 conf]# 

永久修改:

1)/etc/rc.local每次开机都会加载

[root@nginx-kafka01 conf]# vim /etc/rc.local

ulimit -n 1000000

2) 打开/etc/security/limits.conf (内核对Linux某些功能的限制)

#        - nofile - max number of open file descriptors  打开文件描述符的最大数量


*               soft    nofile           1000000
*               hard    nofile           1000000

1.在配置文件里修改配置,新建3个虚拟主机
一个server对应一个网站--》虚拟主机 
 server { 
		listen 80;
		server_name www.song.com;
		location / {
			root html/song;
			index  index.html  index.htm  shouye.html;  首页文件优先级,从最左边看起,没有就依次向右查找
		}
        	access_log  logs/song.access.log  main;
		error_log  logs/song.error.log  notice;
	}

    server { 
		listen 80;
		server_name www.huang.com;
		location / {
			root html/huang;
			index  index.html  index.htm  shouye.html;
		}
        	access_log  logs/huang.access.log  main;
		error_log  logs/huang.error.log  notice;
	}
    server { 
		listen 80;
		server_name www.peng.com;
		location / {
			root html/peng;
			index  index.html  index.htm  shouye.html;
		}
        	access_log  logs/peng.access.log  main;
		error_log  logs/peng.error.log  notice;
	}
2.新建各个虚拟主机对应的网页根目录,并且新建首页
[root@nginx-kafka01 html]# pwd
/usr/local/scpenglinxi99/html


[root@nginx-kafka01 html]# tree
.
├── 50x.html
├── hejin
│   └── he.html
├── huang
│   └── index.html
├── index.html
├── peng
│   └── index.html
└── song
    └── index.html

4 directories, 6 files
3.重启nginx服务

nginx -s reload

4.拿我们的linux服务器做客户机

修改/etc/hosts 添加对应的域名

192.168.218.128 www.peng.com
192.168.218.128 www.song.com
192.168.218.128 www.huang.com

在win7里修改hosts文件
C:\Windows\System32\drivers\etc\hosts

5.测试访问
[root@nginx-kafka01 html]# curl www.huang.com
huangjuan
[root@nginx-kafka01 html]# curl www.song.com
songzhiqiang
[root@nginx-kafka01 html]# curl www.peng.com
penglinxi
虚拟主机的配置 -server
1、基于域名的

2、基于ip的虚拟主机:一个网站对应一个公网IP

3、基于端口的虚拟主机:一个网站一个端口

优点:节省服务器,省钱

缺点:一台虚拟服务器受到攻击,其他的会受到牵连

​ 公用CPU、内存、磁盘、带宽、如果一台服务器的访问量特别大,会导致其他的网站访问的时候异常

域名有什么作用?

​ 方便记忆 www.shaowei.icu -->188.1.1.1 (用户不需要记服务器的ip地址,直接输入域名,域名会解析到对应的公网ip)

为什么网站要备案呢?
防止做非法的事情,例如:传播非法的事情等
中国内的服务器,必须备案
在境外的可以不备案,购买域名和服务器,在域名添加一个名字绑定到服务器的公网ip就可以了

云服务器里面跑了一个nginx ,nginx里面跑了三个虚拟主机,一个虚拟主机就是一个server,分别是www.feng.com、www.li.com、www.he.com,相当于三个人住在一个房间里面,这个房间归置于云服务器所有,云服务器有个公网ip,12.1.1.1,将购买的域名指向我们的公网ip,这样我们的用户输入www.li.com就会指向我们的公网ip, 因为我们输的是www.li.com,所以不会指向其他的虚拟主机。

​ [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ryXUEyhA-1644419071129)(C:\Users\xixi\AppData\Roaming\Typora\typora-user-images\image-20220130110241779.png)]

事情,例如:传播非法的事情等
中国内的服务器,必须备案
在境外的可以不备案,购买域名和服务器,在域名添加一个名字绑定到服务器的公网ip就可以了

云服务器里面跑了一个nginx ,nginx里面跑了三个虚拟主机,一个虚拟主机就是一个server,分别是www.feng.com、www.li.com、www.he.com,相当于三个人住在一个房间里面,这个房间归置于云服务器所有,云服务器有个公网ip,12.1.1.1,将购买的域名指向我们的公网ip,这样我们的用户输入www.li.com就会指向我们的公网ip, 因为我们输的是www.li.com,所以不会指向其他的虚拟主机。

​ [外链图片转存中…(img-ryXUEyhA-1644419071129)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值