–user=nginx \

–group=nginx \

–with-http_stub_status_module && make && make install

2.安装后优化调整

[root@Nginx nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ #创建软链接优化执行路径

[root@Nginx nginx-1.18.0]# cd

[root@Nginx ~]# nginx -v #查看版本

[root@Nginx ~]# nginx #启动Nginx

[root@Nginx ~]# netstat -anpt | grep nginx #查看网络连接状态

3.编写 Nginx 的启动脚本

[root@Nginx ~]# vim /usr/lib/systemd/system/nginx.service

[Unit]

Description=nginx

After=network.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

[root@Nginx ~]# nginx -s stop

[root@Nginx ~]# netstat -anpt | grep 80

[root@Nginx ~]# systemctl start nginx

[root@Nginx ~]# netstat -anpt | grep 80

三、Nginx 优化

=============================================================================

1.配置 CPU 亲和

配置 CPU 亲和能够减少进程之间不断频繁迁移,减少性能损耗。

1)查看当前 CPU 物理状态

[root@Nginx ~]# lscpu | grep “CPU(s)”

2)将 Nginx Worker 进程绑到不同的核心上

指定 CPU 核心来进行绑定:

worker_processes 6;

worker_cpu_affinity 000001 000010 000100 001000 010000 100000;

最佳绑定方式:

worker_processes auto;

worker_cpu_affinity auto;

3)查看 Nginx Worker 进程绑定至对应 CPU

[root@Nginx ~]# ps -eo pid,args,psr | grep [n]ginx

2.Nginx 常见问题

1)Server 优先级

[root@Nginx ~]# mkdir -p /zhangsan/Coco{1…3}

[root@Nginx ~]# for A in {1…3};do echo “

Coco

A

<

/

h

1

>

"

>

/

z

h

a

n

g

s

a

n

/

C

o

c

o

"

A" > /zhangsan/Coco"

A">/zhangsan/Coco"A”/index.html;done

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name 192.168.1.1;

location / {

root /zhangsan/Coco1;

index index.html;

}

}

server {

listen 80;

server_name 192.168.1.1;

location / {

root /zhangsan/Coco2;

index index.html;

}

}

server {

listen 80;

server_name 192.168.1.1;

location / {

root /zhangsan/Coco3;

index index.html;

}

}

}

[root@Nginx ~]# nginx -t #检查配置文件是否正确

[root@Nginx ~]# systemctl restart nginx #重启Nginx服务

[root@Nginx ~]# curl http://192.168.1.1

注意:当多个 server_name 一样时,访问的优先级是从上到下。

2)Location 优先级

一个 Server 出现多个 location

| 常用的正则匹配 | 作用 |

| — | — |

| = | 进行普通字符精确匹配,完全匹配 |

| ^~ | 进行普通字符匹配,使用前缀匹配 |

| ~ | 区分大小写匹配 |

| ~* | 不区分大小写匹配 |

优先级为:= ^~ ~ ~*

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name 192.168.1.1;

root /zhangsan;

index index.html;

location = /Coco1/ {

rewrite ^(.*)$ /Coco1/index.html break;

}

location ~ /Coco* {

rewrite ^(.*)$ /Coco3/index.html break;

}

location ^~ /Coco {

rewrite ^(.*)$ /Coco2/index.html break;

}

}

}

[root@Nginx ~]# systemctl restart nginx

[root@Nginx ~]# curl http://192.168.1.1/Coco1/

Coco 1

[root@Nginx ~]# curl http://192.168.1.1/Cocooo

Coco 2

[root@Nginx ~]# curl http://192.168.1.1/Coc

Coco 3

注意:完全匹配是必须要完全一样才可以,使用前缀匹配是只要前面一样即可。

3)Try_Files 的使用

Nginx 的 Try_Files 能够按顺序检查文件是否存在。

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name 192.168.1.1;

root /zhangsan;

index index.html;

location / {

try_files $uri $uri/ /Coco3/index.html;

}

}

}

[root@Nginx ~]# systemctl restart nginx

[root@Nginx ~]# curl http://192.168.1.1/Coco1/

Coco 1

[root@Nginx ~]# curl http://192.168.1.1/Docker/

Coco 3

执行过程:

首先会检查用户请求的 uri 内容是否存在本地,存在则解析; 如果不存在的话就会根据 /Coco3/index.html 指定路径来解析。

4)Alias 与 Root 区别

Root 路径配置:

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name 192.168.1.1;

location /Coco1 {

root /zhangsan;

index index.html;

}

}

}

[root@Nginx ~]# systemctl restart nginx

[root@Nginx ~]# curl http://192.168.1.1/Coco1/

Coco 1

实际请求本地路径为:root路径 + location路径 也就是 /zhangsan/Coco1/

Alias 路径配置:

[root@Nginx ~]# vim /usr/local/nginx/conf/nginx.conf

http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

server {

listen 80;

server_name 192.168.1.1;

location /Docker {

alias /zhangsan/Coco1; 自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

最后

文章到这里就结束了,如果觉得对你有帮助可以点个赞哦,如果有需要前端校招面试题PDF完整版的朋友可以点击这里即可获取,包括答案解析。

[外链图片转存中…(img-HIaHUjIm-1712985254707)]

[外链图片转存中…(img-y1hMc3kf-1712985254707)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!

[外链图片转存中…(img-NIz5etjA-1712985254708)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:前端)

最后

文章到这里就结束了,如果觉得对你有帮助可以点个赞哦,如果有需要前端校招面试题PDF完整版的朋友可以点击这里即可获取,包括答案解析。

[外链图片转存中…(img-Qzz1Q00s-1712985254708)]

精彩链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: