簡介
Nginx是一個高性能的HTTP和反向代理web服務(wù)器,同時也提供了IMAP/POP3/SMTP服務(wù)。其特點是占有內(nèi)存少,并發(fā)能力強,在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好。此外,Nginx也可以作為一個通用的TCP/UDP代理服務(wù)器。
Nginx是由伊戈爾·賽索耶夫為俄羅斯訪問量第二的Rambler.ru站點開發(fā)的,公開版本1.19.6發(fā)布于2020年12月15日。其將源代碼以類BSD許可證的形式發(fā)布,因它的穩(wěn)定性、豐富的功能集、簡單的配置文件和低系統(tǒng)資源的消耗而聞名。
Nginx可以作為一個HTTP服務(wù)器,將服務(wù)器上的靜態(tài)文件(如HTML、圖片)通過HTTP協(xié)議展現(xiàn)給客戶端。同時,Nginx也可以作為反向代理服務(wù)器,為后端服務(wù)器代理請求。
Nginx應(yīng)用比較多的場景是作為Web服務(wù)器和反向代理服務(wù)器,并且可以通過配置虛擬主機提供HTTP Web服務(wù)。此外,Nginx也可以提供一定的緩存服務(wù)功能。
基于yum
[root@VM-8-17-centos ~]# yum list all | grep nginx
nginx.x86_64 1:1.20.1-10.el7 @epel
nginx-filesystem.noarch 1:1.20.1-10.el7 @epel
collectd-nginx.x86_64 5.8.1-1.el7 epel
munin-nginx.noarch 2.0.69-5.el7 epel
nginx-all-modules.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@VM-8-17-centos ~]# yum install nginx -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
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: 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 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:
gperftools-libs x86_64 2.6.1-1.el7 os 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 (+3 Dependent packages)
Total download size: 2.3 M
Installed size: 6.6 M
Downloading packages:
(1/4): nginx-filesystem-1.20.1-10.el7.noarch.rpm | 24 kB 00:00:00
(2/4): nginx-1.20.1-10.el7.x86_64.rpm | 588 kB 00:00:00
(3/4): gperftools-libs-2.6.1-1.el7.x86_64.rpm | 272 kB 00:00:00
(4/4): openssl11-libs-1.1.1k-5.el7.x86_64.rpm | 1.5 MB 00:00:00
-----------------------------------------------------------------------------------------------------------------
Total 6.9 MB/s | 2.3 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:openssl11-libs-1.1.1k-5.el7.x86_64 1/4
Installing : 1:nginx-filesystem-1.20.1-10.el7.noarch 2/4
Installing : gperftools-libs-2.6.1-1.el7.x86_64 3/4
Installing : 1:nginx-1.20.1-10.el7.x86_64 4/4
Verifying : gperftools-libs-2.6.1-1.el7.x86_64 1/4
Verifying : 1:nginx-filesystem-1.20.1-10.el7.noarch 2/4
Verifying : 1:nginx-1.20.1-10.el7.x86_64 3/4
Verifying : 1:openssl11-libs-1.1.1k-5.el7.x86_64 4/4
Installed:
nginx.x86_64 1:1.20.1-10.el7
Dependency Installed:
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!
[root@VM-8-17-centos ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.1.1k FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'
[root@VM-8-17-centos ~]# 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
[root@VM-8-17-centos nginx]# rpm -ql nginx | grep bin
/usr/bin/nginx-upgrade
/usr/sbin/nginx
[root@VM-8-17-centos nginx]# nginx -h
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
[root@VM-8-17-centos nginx]# netstat -tnlp | grep 80
[root@VM-8-17-centos nginx]# /usr/sbin/nginx
[root@VM-8-17-centos nginx]# netstat -tnlp | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5146/nginx: master
tcp6 0 0 :::80 :::* LISTEN 5146/nginx: master
[root@VM-8-17-centos nginx]# ps -ef | grep nginx
root 5146 1 0 23:19 ? 00:00:00 nginx: master process /usr/sbin/nginx
nginx 5147 5146 0 23:19 ? 00:00:00 nginx: worker process
nginx 5148 5146 0 23:19 ? 00:00:00 nginx: worker process
nginx 5149 5146 0 23:19 ? 00:00:00 nginx: worker process
nginx 5150 5146 0 23:19 ? 00:00:00 nginx: worker process
root 5281 1769 0 23:20 pts/0 00:00:00 grep --color=auto nginx
[root@VM-8-17-centos nginx]# kill -9 5146
[root@VM-8-17-centos nginx]# ps -ef | grep nginx
nginx 5147 1 0 23:19 ? 00:00:00 nginx: worker process
nginx 5148 1 0 23:19 ? 00:00:00 nginx: worker process
nginx 5149 1 0 23:19 ? 00:00:00 nginx: worker process
nginx 5150 1 0 23:19 ? 00:00:00 nginx: worker process
root 5406 1769 0 23:20 pts/0 00:00:00 grep --color=auto nginx
[root@VM-8-17-centos nginx]# kill -9 5147 5148 5149 5150
[root@VM-8-17-centos nginx]# ps -ef | grep nginx
root 5671 1769 0 23:21 pts/0 00:00:00 grep --color=auto nginx
[root@VM-8-17-centos nginx]# /usr/sbin/nginx
[root@VM-8-17-centos nginx]# pwd
/var/log/nginx
[root@VM-8-17-centos nginx]# ll
total 4
-rw-r--r-- 1 root root 0 Jun 21 23:19 access.log
-rw-r--r-- 1 root root 1007 Jun 21 23:23 error.log
[root@VM-8-17-centos nginx]# cat error.log
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to 0.0.0.0:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: bind() to [::]:80 failed (98: Address already in use)
2023/06/21 23:23:02 [emerg] 5986#5986: still could not bind()
[root@VM-8-17-centos nginx]# curl 127.0.0.1
[root@VM-8-17-centos nginx]# cat access.log
127.0.0.1 - - [21/Jun/2023:23:24:31 +0800] "GET / HTTP/1.1" 200 4833 "-" "curl/7.29.0" "-"
127.0.0.1 - - [21/Jun/2023:23:24:48 +0800] "GET / HTTP/1.1" 200 4833 "-" "curl/7.29.0" "-"
[root@VM-8-17-centos nginx]#
基于源碼模式
下載地址:http://nginx.org/en/download.html
版本說明
Mainline version:在線版本,正處于開發(fā)狀態(tài)
Stable version :穩(wěn)定版本(一般下載使用)
Legacy version :遺留版本,遺留的老的版本
mkdir -p /server/download/nginx
cd /server/download/nginx
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzvf nginx-1.24.0.tar.gz
源碼結(jié)構(gòu)
drwxr-xr-x 6 1001 1001 4096 2023-07-31 15:32 auto
-rw-r--r-- 1 1001 1001 323312 2023-04-11 09:45 CHANGES
-rw-r--r-- 1 1001 1001 494234 2023-04-11 09:45 CHANGES.ru
drwxr-xr-x 2 1001 1001 4096 2023-07-31 15:32 conf
-rwxr-xr-x 1 1001 1001 2611 2023-04-11 09:45 configure
drwxr-xr-x 4 1001 1001 4096 2023-07-31 15:32 contrib
drwxr-xr-x 2 1001 1001 4096 2023-07-31 15:32 html
-rw-r--r-- 1 1001 1001 1397 2023-04-11 09:45 LICENSE
drwxr-xr-x 2 1001 1001 4096 2023-07-31 15:32 man
-rw-r--r-- 1 1001 1001 49 2023-04-11 09:45 README
drwxr-xr-x 9 1001 1001 4096 2023-07-31 15:32 src
- src:這是Nginx源碼的根目錄,包含了Nginx的所有源文件。
- core:該目錄下存放了Nginx的核心源碼,包括定義常用數(shù)據(jù)結(jié)構(gòu)、體系結(jié)構(gòu)實現(xiàn)等的文件。
- event:該目錄下存放了Nginx的事件系統(tǒng)源碼。
- http:該目錄下存放了Nginx的HTTP服務(wù)器實現(xiàn)源碼。
- mail:該目錄下存放了Nginx的郵件服務(wù)器實現(xiàn)源碼,主要實現(xiàn)對郵件服務(wù)依賴的數(shù)據(jù)結(jié)構(gòu)的定義和初始化。
- misc:該目錄下存放了一些其他文件,包括測試程序中引用的頭文件是否與C++兼容的文件,以及支持Google PerTools的文件等。
- os:該目錄下包含了針對不同操作系統(tǒng)下的函數(shù)進行封裝以及實現(xiàn)核心調(diào)用的源碼。
- auto:該目錄下存放了一些自動編譯安裝相關(guān)的腳本和配置文件。
- conf:該目錄下存放了Nginx的配置文件以及fcgi相關(guān)的配置等文件。
- contrib:該目錄下存放了一些實用的工具,如geo配置生成工具等。
- html:該目錄下存放了默認(rèn)的網(wǎng)頁文件。
- man:該目錄下存放了Nginx的man手冊。
添加nginx用戶
useradd -s /sbin/nologin nginx -M
編譯nginx
# 安裝 所需依賴
yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 配置 nginx
# --prefix=<path>:定義服務(wù)器文件的完整路徑,該路徑同時也是configure命令設(shè)置的相對路徑(除類庫源文件外)以及nginx.conf文件定義的相對路徑的基準(zhǔn)路徑。其默認(rèn)值是/usr/local/nginx。
# --sbin-path=<path>:設(shè)置nginx可執(zhí)行文件的完整路徑,該路徑僅在安裝期間使用,默認(rèn)路徑為prefix/sbin/nginx。
# --conf-path=<path>:設(shè)置配置文件nginx.conf的完整路徑。如有必要,總是可以在nginx啟動時通過命令行參數(shù)-cfile指定一個不同的配置文件路徑。默認(rèn)路徑為prefix/conf/nginx.conf。
# --pid-path=<path>:設(shè)置nginx.pid文件的完整路徑,該文件存儲了主進程的進程ID。安裝完成后,該路徑總是可以在nginx.conf文件中用pid指令來修改。默認(rèn)路徑為prefix/logs/nginx.pid。
# --error-log-path=<path>:設(shè)置記錄主要錯誤、警告以及調(diào)試信息日志的完整路徑。安裝完成后,該路徑總是可以在nginx.conf文件中用error_log指令來修改。默認(rèn)路徑為prefix/logs/error.log。
# --http-log-path=<path>:設(shè)置記錄HTTP服務(wù)器主要請求日志的完整路徑。安裝完成后,該路徑總是可以在nginx.conf文件用access_log指令來修改。默認(rèn)路徑為prefix/logs/access.log。
# --user=name:設(shè)置工作進程以某非特權(quán)用戶的身份運行。安裝完成后,該用戶名總是可以在nginx.conf文件用user指令來修改。其默認(rèn)值為nobody。
# --group=name:設(shè)置工作進程以某非特權(quán)用戶的身份運行。安裝完成后,該用戶名總是可以在nginx.conf用user指令來修改。其默認(rèn)名稱與未授權(quán)用戶名稱相同。
# 在Nginx的配置中,“with”和“without”參數(shù)用于指定是否包含特定的模塊或功能。使用“with”參數(shù)可以啟用某個模塊或功能,使用“without”參數(shù)可以禁用某個模塊或功能
./configure --user=nginx \
--group=nginx \
--prefix=/server/application/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx-error.log \
--pid-path=/var/run/nginx.pid \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-pcre \
# 編譯并且安裝
make && make install
# 配置環(huán)境變量
echo '#nginx config.'>>/etc/profile
echo 'export PATH=$PATH:/server/application/nginx/sbin'>> /etc/profile
source /etc/profile
# 配置nginx語法高亮
cp -r contrib/vim/ ~/.vim/
cat >~/.vim/filetype.vim <<EOF
au BufRead,BufNewFile /etc/nginx/* set ft=nginx
EOF
nginx -V
nginx 命令行參數(shù)
[root->mcode-server->/server/download/nginx/nginx-1.24.0]# nginx -h
nginx version: nginx/1.24.0
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: /server/application/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
-c
或--conf-file
:指定配置文件的路徑。-t
或--test-config
:測試配置文件的語法是否正確。-p
或--prefix
:設(shè)置Nginx的安裝目錄。-s
或--signal
:向主進程發(fā)送信號。-v
或--version
:顯示Nginx的版本信息。-V
或--verbose
:顯示Nginx的版本、編譯器版本和配置參數(shù)。-g
或--global
:設(shè)置全局配置指令。