今天继续学习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地址 |
connection | TCP连接的序号,递增 |
connection_request | TCP连接当前的请求数量 |
uri | 请求的URL,不包含参数 |
request ur | 请求的URL,包含参数 |
scheme | 协议名,http或https |
request metho | 请求方法 |
request_length | 全部请求的长度,包含请求行、请求头、请求体 |
args | 全部参数字符串 |
arg_参数名 | 获取特定参数值 |
is_args | URL中是否有参数,有的话返回?,否则返回空 |
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 | 返回响应时的速度上限值 |