nginx笔记
nginx概述
历史背景
Nginx (engine x)是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文: PaMOnep)开发的,第一个公开版本0.1.0发布于2004年10月4日。
其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
应用场景
高性能的静态WEB服务器
反向代理
Nginx为什么出现如此流行:互联网数据的快速增长
Apache处理请求的低效性
Nginx相较于Apache的优势
Apache | nginx |
---|---|
一个进程处理一个请求 | 一个进程处理多个请求 |
阻塞式的 | 非阻塞式的 |
一个Http请求的全流程剖析
三个常用的应用场景
静态资源服务(Web Server)
反向代理服务
API服务
主要特点
高并发、高性能
扩展性好
异步非阻塞的事件驱动模型
高可靠性
热部署
部署使用
环境:centos7.9
[root@localhost ~]# yum -y install epel-release
Loaded plugins: fastestmirror
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 249 kB 00:00:00
(2/4): base/7/x86_64/group_gz | 153 kB 00:00:00
(3/4): base/7/x86_64/primary_db | 6.1 MB 00:00:00
(4/4): updates/7/x86_64/primary_db | 20 MB 00:00:05
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
epel-release noarch 7-11 extras 15 k
Transaction Summary
========================================================================================
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Downloading packages:
warning: /var/cache/yum/x86_64/7/extras/packages/epel-release-7-11.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Public key for epel-release-7-11.noarch.rpm is not installed
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Importing GPG key 0xF4A80EB5:
Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
Package : centos-release-7-9.2009.0.el7.centos.x86_64 (@anaconda)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/1
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!
## 换epel的国内源
[root@localhost ~]# sudo sed -e 's!^metalink=!#metalink=!g' \
> -e 's!^#baseurl=!baseurl=!g' \
> -e 's!http://download\.fedoraproject\.org/pub/epel!https://mirrors.bfsu.edu.cn/epel!g' \
> -e 's!http://download\.example/pub/epel!https://mirrors.bfsu.edu.cn/epel!g' \
> -i /etc/yum.repos.d/epel*.repo
[root@localhost ~]# yum list all | grep nginx
collectd-nginx.x86_64 5.8.1-1.el7 epel
munin-nginx.noarch 2.0.72-1.el7 epel
nginx.x86_64 1:1.20.1-10.el7 epel
nginx-all-modules.noarch 1:1.20.1-10.el7 epel
nginx-filesystem.noarch 1:1.20.1-10.el7 epel
nginx-mod-devel.x86_64 1:1.20.1-10.el7 epel
nginx-mod-http-image-filter.x86_64 1:1.20.1-10.el7 epel
nginx-mod-http-perl.x86_64 1:1.20.1-10.el7 epel
nginx-mod-http-xslt-filter.x86_64 1:1.20.1-10.el7 epel
nginx-mod-mail.x86_64 1:1.20.1-10.el7 epel
nginx-mod-stream.x86_64 1:1.20.1-10.el7 epel
pagure-web-nginx.noarch 5.13.3-2.el7 epel
pcp-pmda-nginx.x86_64 4.3.2-13.el7_9 updates
python2-certbot-nginx.noarch 1.11.0-1.el7 epel
sympa-nginx.x86_64 6.2.70-2.el7 epel
[root@localhost ~]# yum -y install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
epel | 4.7 kB 00:00:00
Not using downloaded epel/repomd.xml because it is older than what we have:
Current : Tue Apr 4 23:52:32 2023
Downloaded: Tue Mar 28 23:02:34 2023
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.1-10.el7 for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-10.el7.x86_64
--> Running transaction check
---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-filesystem.noarch 1:1.20.1-10.el7 will be installed
---> Package openssl11-libs.x86_64 1:1.1.1k-5.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================
Package Arch Version Repository Size
========================================================================================
Installing:
nginx x86_64 1:1.20.1-10.el7 epel 588 k
Installing for dependencies:
centos-indexhtml noarch 7-9.el7.centos base 92 k
gperftools-libs x86_64 2.6.1-1.el7 base 272 k
nginx-filesystem noarch 1:1.20.1-10.el7 epel 24 k
openssl11-libs x86_64 1:1.1.1k-5.el7 epel 1.5 M
Transaction Summary
========================================================================================
Install 1 Package (+4 Dependent packages)
Total download size: 2.4 M
Installed size: 6.7 M
Downloading packages:
(1/5): centos-indexhtml-7-9.el7.centos.noarch.rpm | 92 kB 00:00:00
(2/5): gperftools-libs-2.6.1-1.el7.x86_64.rpm | 272 kB 00:00:00
warning: /var/cache/yum/x86_64/7/epel/packages/nginx-filesystem-1.20.1-10.el7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Public key for nginx-filesystem-1.20.1-10.el7.noarch.rpm is not installed
(3/5): nginx-filesystem-1.20.1-10.el7.noarch.rpm | 24 kB 00:00:00
(4/5): nginx-1.20.1-10.el7.x86_64.rpm | 588 kB 00:00:00
(5/5): openssl11-libs-1.1.1k-5.el7.x86_64.rpm | 1.5 MB 00:00:00
----------------------------------------------------------------------------------------
Total 4.1 MB/s | 2.4 MB 00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
Userid : "Fedora EPEL (7) <epel@fedoraproject.org>"
Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
Package : epel-release-7-11.noarch (@extras)
From : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64 1/5
Installing : 1:nginx-filesystem-1.20.1-10.el7.noarch 2/5
Installing : centos-indexhtml-7-9.el7.centos.noarch 3/5
Installing : gperftools-libs-2.6.1-1.el7.x86_64 4/5
Installing : 1:nginx-1.20.1-10.el7.x86_64 5/5
Verifying : gperftools-libs-2.6.1-1.el7.x86_64 1/5
Verifying : centos-indexhtml-7-9.el7.centos.noarch 2/5
Verifying : 1:nginx-filesystem-1.20.1-10.el7.noarch 3/5
Verifying : 1:nginx-1.20.1-10.el7.x86_64 4/5
Verifying : 1:openssl11-libs-1.1.1k-5.el7.x86_64 5/5
Installed:
nginx.x86_64 1:1.20.1-10.el7
Dependency Installed:
centos-indexhtml.noarch 0:7-9.el7.centos gperftools-libs.x86_64 0:2.6.1-1.el7
nginx-filesystem.noarch 1:1.20.1-10.el7 openssl11-libs.x86_64 1:1.1.1k-5.el7
Complete!
#rpm查询nginx包
[root@localhost ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx-1.20.1
/usr/share/doc/nginx-1.20.1/CHANGES
/usr/share/doc/nginx-1.20.1/README
/usr/share/doc/nginx-1.20.1/README.dynamic
/usr/share/doc/nginx-1.20.1/UPGRADE-NOTES-1.6-to-1.10
/usr/share/licenses/nginx-1.20.1
/usr/share/licenses/nginx-1.20.1/LICENSE
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/404.html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/en-US
/usr/share/nginx/html/icons
/usr/share/nginx/html/icons/poweredby.png
/usr/share/nginx/html/img
/usr/share/nginx/html/index.html
/usr/share/nginx/html/nginx-logo.png
/usr/share/nginx/html/poweredby.png
/usr/share/nginx/modules
/usr/share/vim/vimfiles/ftdetect/nginx.vim
/usr/share/vim/vimfiles/ftplugin/nginx.vim
/usr/share/vim/vimfiles/indent/nginx.vim
/usr/share/vim/vimfiles/syntax/nginx.vim
/var/lib/nginx
/var/lib/nginx/tmp
/var/log/nginx
/var/log/nginx/access.log
/var/log/nginx/error.log
# 查看nginx文件
[root@localhost nginx]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx; #指定用户nginx启动
worker_processes auto;
error_log /var/log/nginx/error.log; #错误日志
pid /run/nginx.pid; #程序运行的PID文件
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf; #配置
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
#查nginx启动文件
[root@localhost nginx]# rpm -ql nginx | grep bin
/usr/bin/nginx-upgrade
/usr/sbin/nginx
#查看帮助文档
[root@localhost nginx]# /usr/sbin/nginx -help
nginx version: nginx/1.20.1
Usage: nginx [-?hvVtTq] [-s signal] [-p prefix]
[-e filename] [-c filename] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/share/nginx/)
-e filename : set error log file (default: /var/log/nginx/error.log)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
#启动nginx
[root@localhost nginx]# /usr/sbin/nginx
#再次启动时80端口被占用
[root@localhost nginx]# /usr/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
^C
# 安装net-tools工具
[root@localhost nginx]# yum -y install net-tools
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package net-tools.x86_64 0:2.0-0.25.20131004git.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================
Package Arch Version Repository
Size
==================================================================
Installing:
net-tools x86_64 2.0-0.25.20131004git.el7 base 306 k
Transaction Summary
==================================================================
Install 1 Package
Total download size: 306 k
Installed size: 917 k
Downloading packages:
net-tools-2.0-0.25.20131004git.el7.x86_64.rp | 306 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1
Verifying : net-tools-2.0-0.25.20131004git.el7.x86_64 1/1
Installed:
net-tools.x86_64 0:2.0-0.25.20131004git.el7
Complete!
# 使用netstat工具查看被占用的端口信息
root@localhost nginx]# netstat -tlnp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8611/nginx: master
tcp6 0 0 :::80 :::* LISTEN 8611/nginx: master
# 使用kill命令结束进程
[root@localhost nginx]# kill -9 8612
#
[root@localhost nginx]# netstat -tlnp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8613/nginx: worker
tcp6 0 0 :::80 :::* LISTEN 8613/nginx: worker
# 使用ps查看进程
[root@localhost nginx]# ps -ef | grep nginx
nginx 8613 1 0 08:44 ? 00:00:00 nginx: worker process
nginx 8614 1 0 08:44 ? 00:00:00 nginx: worker process
nginx 8615 1 0 08:44 ? 00:00:00 nginx: worker process
nginx 8616 1 0 08:44 ? 00:00:00 nginx: worker process
nginx 8617 1 0 08:44 ? 00:00:00 nginx: worker process
root 8646 8414 0 08:49 pts/0 00:00:00 grep --color=auto nginx
# 强制结束nginx相关进程
[root@localhost nginx]# kill -9 8613 8614 8615 8616 8617
# 再使用ps查看进程
[root@localhost nginx]# ps -ef | grep nginx
root 8648 8414 0 08:50 pts/0 00:00:00 grep --color=auto nginx
# 再使用netstat工具查看nginx端口占用情况
[root@localhost nginx]# netstat -tlnp | grep nginx
[root@localhost nginx]#
# 再次启动nginx服务
[root@localhost nginx]# /usr/sbin/nginx
[root@localhost nginx]#
[root@localhost nginx]# ps -ef | grep nginx
root 8653 1 0 08:54 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 8654 8653 0 08:54 ? 00:00:00 nginx: worker process
nginx 8655 8653 0 08:54 ? 00:00:00 nginx: worker process
nginx 8656 8653 0 08:54 ? 00:00:00 nginx: worker process
nginx 8657 8653 0 08:54 ? 00:00:00 nginx: worker process
nginx 8658 8653 0 08:54 ? 00:00:00 nginx: worker process
nginx 8659 8653 0 08:54 ? 00:00:00 nginx: worker process
root 8661 8414 0 08:54 pts/0 00:00:00 grep --color=auto nginx
[root@localhost nginx]# kill -9 8653 8654 8655 8656 8657 8658 8659
[root@localhost nginx]# ps -ef | grep nginx
root 8689 8414 0 08:57 pts/0 00:00:00 grep --color=auto nginx
# 使用-c参数指定nginx配置文件启动nginx服务
[root@localhost nginx]# /usr/sbin/nginx -c /etc/nginx/nginx.conf
[root@localhost nginx]#
# 使用ps命令
[root@localhost nginx]# ps -ef | grep nginx
root 8692 1 0 08:58 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx 8693 8692 0 08:58 ? 00:00:00 nginx: worker process
nginx 8694 8692 0 08:58 ? 00:00:00 nginx: worker process
nginx 8695 8692 0 08:58 ? 00:00:00 nginx: worker process
nginx 8696 8692 0 08:58 ? 00:00:00 nginx: worker process
nginx 8697 8692 0 08:58 ? 00:00:00 nginx: worker process
nginx 8698 8692 0 08:58 ? 00:00:00 nginx: worker process
root 8700 8414 0 09:00 pts/0 00:00:00 grep --color=auto nginx
# 查看nginx日志文件
[root@localhost nginx]# cd /var/log/nginx/
[root@localhost nginx]# ls
access.log error.log
#输入ip地址访问Nginx页面
#若无法访问则需要检查firewalld是否被关闭
#查看access.log访问日志
[root@localhost nginx]# cat access.log
192.168.100.1 - - [05/Apr/2023:09:02:57 -0400] "GET / HTTP/1.1" 200 4833 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62" "-"
192.168.100.1 - - [05/Apr/2023:09:02:57 -0400] "GET /img/centos-logo.png HTTP/1.1" 200 3030 "http://192.168.100.132/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62" "-"
192.168.100.1 - - [05/Apr/2023:09:02:57 -0400] "GET /img/html-background.png HTTP/1.1" 200 1801 "http://192.168.100.132/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62" "-"
192.168.100.1 - - [05/Apr/2023:09:02:57 -0400] "GET /img/header-background.png HTTP/1.1" 200 82896 "http://192.168.100.132/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62" "-"
192.168.100.1 - - [05/Apr/2023:09:02:57 -0400] "GET /favicon.ico HTTP/1.1" 404 3650 "http://192.168.100.132/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62" "-"
[root@localhost nginx]#
#可以使用tail -f 动态的去查看访问日志