当前位置: 首页 > news >正文

今天继续学习nginx服务部署与配置

一.完整的nginx编译安装的步骤及优化

1.软件包拉到windterm里,这里我复制的是nginx-1.29.1版本

2.安装nginx依赖文件lrzsz,pcre-devel,zlib-devel

yum install -y lrzsz
yum install -y pcre-devel
yum install -y zlib-devel

3.生成nginx配置文件编译到/usr/local/nginx29/文件,在make环境下安装install

./configure --prefix=/usr/local/nginx29 && make && make install

4.测试nginx(用curl访问一个网站)

[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ls -lh
总计 3.9M
-rwxr-xr-x. 1 root root 3.9M  9月23日 15:55 nginx
[root@localhost sbin]# ./nginx -V
nginx version: nginx/1.29.1
built by gcc 12.3.1 (openEuler 12.3.1-62.oe2403sp1) (GCC) 
configure arguments: --prefix=/usr/local/nginx29
[root@localhost sbin]# ./nginx 
[root@localhost sbin]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

5.创建nginx软链接就能直接使用nginx命令了

[root@localhost sbin]# ./nginx -s stop
[root@localhost sbin]# ln -s /usr/local/nginx29/sbin/nginx /usr/local/sbin/
[root@localhost sbin]# nginx
[root@localhost sbin]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p><p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p><p><em>Thank you for using nginx.</em></p>
</body>
</html>

6.添加模块功能

cd nginx-1.29.1/
ls
useradd nginx
usermod -s /sbin/nologin nginx
rm -rf /usr/local/nginx29/
./configure --prefix=/usr/local/nginx29 --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-openssl-opt=yes --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug
yum install -y libxml2-devel
./configure --prefix=/usr/local/nginx29 --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-openssl-opt=yes --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug
make

先删除文件内容再往里添加模块(避免重复),然后添加模块不断试错,成功后make编译

二.nginx.conf配置文件

打开nginx

使用 ps aux | grep nginx 可以列出当前系统中与 Nginx 相关的进程信息,包括主进程和工作进程。

[root@localhost ~]# nginx
[root@localhost ~]# ps aux | grep nginx
root       46121  0.0  0.5  10040  2276 ?        Ss   16:56   0:00 nginx: master process nginx
nginx      46122  0.0  1.0  11808  4368 ?        S    16:56   0:00 nginx: worker process
root       46178  0.0  0.5  21988  2248 pts/1    S+   16:56   0:00 grep --color=auto nginx

修改nginx.conf 和添加404.html文件并给它加上未匹配上的内容

[root@localhost conf]# mkdir conf.d default.d
[root@localhost conf]# vim nginx.conf
[root@localhost html]# vim 404.html

需要改动的地方

user  nginx;
worker_processes  2;#error_log  logs/error.log;
error_log   /usr/local/nginx29/logs/error.log  notice;
#error_log  logs/error.log  info;pid        /usr/local/nginx29/logs/nginx.pid;
gzip  on;
location / {root   /usr/local/nginx29/html;index  index.html index.htm;}error_page  404              /404.html;location = /404.html {root    /usr/local/nginx29/html;}# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root  /usr/local/nginx29/html;}

重启nginx服务

[root@localhost conf]# nginx -s reload

三.HTTPS访问配置[自签名]

SSL简介

主要任务是提供私密性,信息完整性和身份认证。

传输过程

  • 首先发送数字证书,获取到可信的public key。

  • 通过public key发送对称秘钥,接收方用私钥解密。

  • 通信双方使用仅仅对方可知的对称秘钥进行加密传输。

HTTPS 工作流程(背会)

  • 客户端(浏览器)访问 https://www.baidu.com 百度网站;

  • 百度服务器返回 HTTPS 使用的 CA 证书;

  • 浏览器验证 CA 证书是否为合法证书;

  • 验证通过,证书合法,生成一串随机数并使用公钥(证书中提供的)进行加密;

  • 发送公钥加密后的随机数给百度服务器;

  • 百度服务器拿到密文,通过私钥进行解密,获取到随机数(公钥加密,私钥解密,反之也可以);

  • 百度服务器把要发送给浏览器的内容,使用随机数进行加密后传输给浏览器;(对称加密)

  • 此时浏览器可以使用随机数进行解密,获取到服务器的真实传输内容。

测试

[root@localhost pki]# curl -k https://192.168.60.161

四.location配置

location [ = | ~ | ~* | ^~ ] uri {
  ...
}
##在浏览器中输入的:http://www.jx.com/s?id=1&test=123,称作URL,即:统一资源定位符
##在WEB服务器中,对资源进行标识,URI,即统一资源标识符

  • = 精确匹配;

  • ~ 正则匹配,区分大小写;

  • ~* 正则匹配,不区分大小写;

  • ^~ 匹配到即停止搜索;

匹配优先级

= > ^~ > ~ > ~* > 不带任何字符 “/”

配置案例

location / {root   /usr/local/nginx29/html;index  index.html index.htm;}location = /info.html {root   /usr/local/nginx29/html;}location ~ \.(png|jpeg|gif|svg|jpg)$ {root   /usr/local/nginx29/html/images;}location ^~ /bbs/ {root   /usr/local/nginx29/html;index  index.html index.htm;}error_page  404              /404.html;location = /404.html {root    /usr/local/nginx29/html;}

五.rewrite配置

根据指定正则表达式匹配规则,重写 URL 。应用场景: 新老域名的更替!!!

可写入字段

server、location、if

nginx配置中的常用变量(全部背会)

变量名含义
remote_add客户端IP地址
remote_port客户端端口
server_addr服务端IP地址
Server_port服务端端口
server_protocol服务端协议
binary_remote_addr二进制格式的客户端IP地址
connectionTCP连接的序号,递增
connection_requestTCP连接当前的请求数量
uri请求的URL,不包含参数
request ur请求的URL,包含参数
scheme协议名,http或https
request metho请求方法
request_length全部请求的长度,包含请求行、请求头、请求体
args全部参数字符串
arg_参数名获取特定参数值
is_argsURL中是否有参数,有的话返回?,否则返回空
query_string与args相同
host请求信息中的Host,如果请求中没有Host行,则在请求头中找,最后 使用nginx中设置的server_name。
http_user_agent用户访问方式
http_referer从哪些链接过来的请求
http_via每经过一层代理服务器,都会添加相应的信息
http_cookie获取用户cookie
request time处理请求已消耗的时间
https是否开启了https,是则返回on,否则返回空
request_filename磁盘文件系统待访问文件的完整路径
document_root由URI和root/alias规则生成的文件夹路径
limit_rate返回响应时的速度上限值
http://www.dtcms.com/a/398274.html

相关文章:

  • flutter 编译报错java.util.zip.ZipException: zip END header not found
  • 网站建设精英京东商城网站域名
  • 《AI工具驱动的分布式任务调度系统从0到1实践解析》
  • C#练习——事件
  • 深拷贝浅拷贝的区别?如何实现⼀个深拷贝?
  • C primer plus (第六版)第十一章 编程练习第10题
  • AgentScope Studio 安装与测试
  • 长沙房产交易中心官网做seo网站空间
  • 金融培训网站源码淘宝基地网站怎么做
  • Spark核心Storage详解
  • 高系分二十:微服务系统分析与设计
  • 深度学习----ResNet(残差网络)-彻底改变深度神经网络的训练方式:通过残差学习来解决深层网络退化问题(附PyTorch实现)
  • 脑电模型实战系列:入门脑电情绪识别-用最简单的DNN模型起步
  • 赣州企业网站建设比较火的推广软件
  • 广州公司网站制作网页游戏排行榜20
  • 算法提升之单调数据结构-(单调队列)
  • PHP 线上环境 Composer 依赖包更新部署指南-简易版
  • 设计模式-原型模式详解
  • ESP8266与CEM5826-M11毫米波雷达传感器的动态检测系统
  • [原创]怎么用qq邮箱订阅arxiv.org?
  • 设计模式-中介者模式详解
  • 【探寻C++之旅】第十四章:简单实现set和map
  • 牛客:机器翻译
  • 20250925的学习笔记
  • 域名不同网站程序相同wordpress多门户网站
  • 淘宝API商品详情接口全解析:从基础数据到深度挖掘
  • 【低代码】百度开源amis
  • 求推荐专业的网站建设开发免费商城
  • java面试day4 | 微服务、Spring Cloud、注册中心、负载均衡、CAP、BASE、分布式接口幂等性、xxl-job
  • 高QE sCMOS相机在SIM超分辨显微成像中的应用