搜尋
首頁運維Nginxnginx快速入門

nginx快速入門

Jan 27, 2021 am 10:12 AM
nginx

nginx快速入門

nginx簡單介紹:

(學習影片分享:程式設計入門

Nginx (engine x) 是一款輕量級的Web 伺服器、反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器。它是來自俄羅斯的Igor Sysoev在為Rambler Media工作期間,使用C語言開發的。

Igor Sysoev將Nginx的程式碼開源,並且賦予其最自由的2-clause BSD-like license許可證。由於Nginx使用基於事件驅動的架構能夠並發處理百萬級的TCP連接,高度模組化的設計和自由的許可證使得擴展Nginx功能的第三方模組層出不窮,而且優秀的設計帶來了極佳的穩定性,因此其作為Web伺服器被廣泛應用到大流量的網站上。

所謂反向代理(Reverse Proxy)方式是指以代理伺服器來接受internet 上的連接請求,然後將請求轉發給內部網路上的伺服器,並將從伺服器上得到的結果傳回給internet上請求連線的客戶端,此時代理伺服器對外就表現為一個反向代理伺服器。

既然有反向代理,那麼也就有正向代理。正向代理程式是位於客戶端和原始伺服器之間的伺服器,為了從原始伺服器取得內容,客戶端向代理程式發送請求並指定目標,然後代理程式向原始伺服器轉交請求並將獲得的內容傳回給客戶端。

可以說正向代理代理的是客戶端,反向代理代理的是伺服器。

nginx快速入門

使用Nginx有下列優點:

nginx快速入門

依賴函式庫

現在伺服器一般都會使用Linux作業系統,在編譯和安裝Nginx之前,你需要先安裝其依賴的函式庫。

以下列舉幾個完成Web伺服器最基本功能所必需的程式庫。

GCC

GCC(GNU Compiler Collection)可用於編譯C語言程式。

Nginx通常不會直接提供二進位可執行程序,因此我們需要編譯其原始碼。

而且我們可能會使用C 來寫Nginx HTTP模組,這時就需要用到G 編譯器了。

用yum安裝G 編譯器:

yum install -y gcc-c++

PCRE

PCRE函式庫PCRE(Perl Compatible Regular Expressions,Perl相容正規表示式)是由Philip Hazel開發的函式庫,目前為許多軟體所使用,該程式庫支援正規表示式。它是由RegEx演化而來,實際上, Perl正規表示式也是源自於Henry Spencer寫的RegEx。

如果我們在設定檔nginx.conf裡使用了正規表示式,那麼在編譯Nginx時就必須把PCRE函式庫編譯進Nginx,因為Nginx的HTTP模組要靠它來解析正規表示式。

當然,如果你確認不會使用正規表示式,就不必安裝它。

其yum安裝方式如下:

yum install -y pcre pcre-devel

pcre-devel是使用PCRE做二次開發時所需要的開發函式庫,包括頭檔等,這也是編譯Nginx所必須使用的。

zlib函式庫

zlib函式庫用於對HTTP套件的內容做gzip格式的壓縮,如果我們在nginx.conf裡配置了gzip on, 並指定對於某些類型(content-型)的HTTP回應使用gzip來進行壓縮以減少網路傳輸量,那麼,編譯時就必須把zlib編譯進Nginx。

其yum安裝方式如下

yum install -y zlib zlib-devel

同理,zlib是直接使用的函式庫,zlib-devel是二次開發所需的函式庫。

OpenSSL開發庫

如果我們的伺服器不只是要支援HTTP,還需要在更安全的SSL協定上傳輸HTTP,那麼就需要擁有OpenSSL了。

另外,如果我們想使用MD5、SHA1等雜湊函數,那麼也需要安裝它。

其yum安裝方式如下:

yum install -y openssl openssl-devel

下載原始碼包

#進入Nginx官方網站的下載介面,選擇最新的穩定版本。

然後使用wget 指令下載:

[root@host nginx]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
--2019-05-23 03:28:52--  http://nginx.org/download/nginx-1.16.0.tar.gz
Resolving nginx.org... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1032345 (1008K) [application/octet-stream]
Saving to: “nginx-1.16.0.tar.gz”

100%[==========================================================================================================================================>] 1,032,345    715K/s   in 1.4s    

2019-05-23 03:28:53 (715 KB/s) - “nginx-1.16.0.tar.gz” saved [1032345/1032345]

解壓縮檔:

[root@host nginx]# tar xf nginx-1.16.0.tar.gz 
[root@host nginx]# ls
nginx-1.16.0  nginx-1.16.0.tar.gz
[root@host nginx]# cd nginx-1.16.0
[root@host nginx-1.16.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

編譯安裝

編譯並安裝Nginx使用下面三條指令:

./configure make make install

如果你依賴的函式庫找不到的話,在執行./configure指令的時候會報錯,例如找不到PCRE函式庫:

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.

正常的輸出應該是下面這樣,並且產生了Makefile :

[root@host nginx-1.16.0]# ./configure
checking for OS
 + Linux 4.10.4-1.el6.elrepo.i686 i686
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) 
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for gcc builtin 64 bit byteswap ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for EPOLLEXCLUSIVE ... not found
checking for O_PATH ... not found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for prctl(PR_SET_KEEPCAPS) ... found
checking for capabilities ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for sched_setaffinity() ... found
checking for SO_SETFIB ... not found
checking for SO_REUSEPORT ... found
checking for SO_ACCEPTFILTER ... not found
checking for SO_BINDANY ... not found
checking for IP_TRANSPARENT ... found
checking for IP_BINDANY ... not found
checking for IP_BIND_ADDRESS_NO_PORT ... not found
checking for IP_RECVDSTADDR ... not found
checking for IP_SENDSRCADDR ... not found
checking for IP_PKTINFO ... found
checking for IPV6_RECVPKTINFO ... found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... not found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 4 bytes
checking for long long size ... 8 bytes
checking for void * size ... 4 bytes
checking for uint32_t ... found
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 4 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 4 bytes
checking for AF_INET6 ... found
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for pwritev() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for clock_gettime(CLOCK_MONOTONIC) ... not found
checking for clock_gettime(CLOCK_MONOTONIC) in librt ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... not found
checking for zlib library ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

查看Nginx版本

安裝成功以後,可以透過-v參數查看Nginx版本。

[root@host sbin]# /usr/local/nginx/sbin/nginx -v nginx version: nginx/1.16.0

啟動

Nginx支援直接啟動,也支援帶參數啟動,以下分別示範一下。

端口佔用

Nginx需要使用80端口,如果80端口被佔用,啟動會有如下報錯:

nginx: [emerg] bind() to 0.0.0.0: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 0.0.0.0: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 0.0.0.0:80 failed (98: Address already in use)

可以使用lsof工具查看端口佔用情況,如果你沒有裝,可以使用以下指令安裝:

yum install -y lsof

查看本機80埠的佔用情況,並殺掉佔用的進程:

[root@host sbin]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    1765 root   53u  IPv6  15062      0t0  TCP *:http (LISTEN)
[root@host sbin]# killall -9 java
[root@host sbin]# lsof -i :80
[root@host sbin]#

預設啟動

使用whereis命令查看nginx的安装目录:

[root@host nginx-1.16.0]# whereis nginx
nginx: /usr/local/nginx

如果不加任何参数启动,会使用默认的nginx.conf配置文件启动Nginx:

/usr/local/nginx/sbin/nginx

启动成功以后,再请求服务器的时候可以看到包含下面内容的网页:

Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

带参数启动

-c参数指定配置文件的启动方式:

./nginx -c mynginx.conf

-p参数指定Nginx的安装目录:

./nginx -p mydir/nginx

-g参数临时指定一些全局配置项

./nginx -g "pid varnginx/test.pid;"

上面这行命令意味着会把pid文件写到varnginx/test.pid中。

-g参数的约束条件是指定的配置项不能与默认路径下的nginx.conf中的配置项相冲突,否则无法启动。

就像上例那样,类似这样的配置项:pid logs/nginx.pid,是不能存在于默认的nginx.conf中的。

另一个约束条件是,以-g方式启动的Nginx服务执行其他命令行时,需要把-g参数也带上,否则可能出现配置项不匹配的情形。

在不启动Nginx的情况下,使用-t参数仅测试配置文件是否有错误。 例如:

./nginx -t

执行结果中显示配置是否正确。

[root@host sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

测试配置选项时,使用-q参数可以不把error级别以下的信息输出到屏幕。 例如:

./nginx -t -q

停止服务

停止Nginx的服务主要有两种方式。

一种是快速停止,即立即停止Nginx服务正在处理的所有网络请求,马上丢弃连接停止服务。

另外一种是平缓地停止,即允许Nginx处理完当前的请求,但不再接收新的请求,之后再关闭连接,停止工作。

快速停止服务

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

kill服务

kill -s SIGTERM 进程ID或kill -s SIGINT 进程ID与上面./nginx -s stop命令的效果是一样的。

[root@host sbin]# ps -ef|grep nginx 
root     10568     1  0 04:22 ?        00:00:00 nginx: master process ./nginx
nobody   10569 10568  0 04:22 ?        00:00:00 nginx: worker process
root     10571  5440  0 04:23 pts/1    00:00:00 grep nginx
[root@host sbin]# kill -s SIGINT 10568
[root@host sbin]# ps -ef|grep nginx 
root     10574  5440  0 04:24 pts/1    00:00:00 grep nginx
[root@host sbin]#

优雅地停止服务

如果希望Nginx服务可以正常地处理完当前所有请求再停止服务,那么可以使用-s quit参数来停止服务。

例如:

./nginx -s quit

该命令与快速停止Nginx服务是有区别的。

当快速停止服务时,worker进程与master进程在收到信号后会立刻跳出循环,退出进程。

而“优雅”地停止服务时,首先会关闭监听端口,停止接收新的连接,然后把当前正在处理的连接全部处理完,最后再退出进程。

与快速停止服务相似,可以直接发送QUIT信号给master进程来停止服务,其效果与执行-s quit命令是一样的。

例如:

kill -s SIGQUIT <nginx master pid>

如果希望“优雅”地停止某个worker进程,那么可以通过向该进程发送WINCH信号来停止服务 。

例如:

kill -s SIGWINCH <nginx worker pid>

发送信号

./nginx -g TERM | INT | QUIT

TERM 和 INT 信号用于快速停止,QUIT 信号用于平滑停止。

Nginx重新加载配置

使运行中的Nginx重读配置项并生效

使用-s reload参数可以使运行中的Nginx服务重新加载nginx.conf文件。 例如:

usrlocal/nginx/sbin/nginx -s reload

日志文件回滚

使用-s reopen参数可以重新打开日志文件,这样可以先把当前日志文件改名或转移到其他目录中进行备份,再重新打开时就会生成新的日志文件。

这个功能使得日志文件不至于过大。 例如:

./nginx -s reopen

这与使用kill命令发送USR1信号效果相同。

kill -s SIGUSR1 <nginx master pid>

相关推荐:nginx教程

以上是nginx快速入門的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:掘金。如有侵權,請聯絡admin@php.cn刪除
NGINX與Apache:性能,可伸縮性和效率NGINX與Apache:性能,可伸縮性和效率Apr 19, 2025 am 12:05 AM

NGINX和Apache都是強大的Web服務器,各自在性能、可擴展性和效率上有獨特的優勢和不足。 1)NGINX在處理靜態內容和反向代理時表現出色,適合高並發場景。 2)Apache在處理動態內容時表現更好,適合需要豐富模塊支持的項目。選擇服務器應根據項目需求和場景來決定。

終極攤牌:nginx vs. apache終極攤牌:nginx vs. apacheApr 18, 2025 am 12:02 AM

NGINX適合處理高並發請求,Apache適合需要復雜配置和功能擴展的場景。 1.NGINX採用事件驅動、非阻塞架構,適用於高並發環境。 2.Apache採用進程或線程模型,提供豐富的模塊生態系統,適合複雜配置需求。

nginx行動:示例和現實應用程序nginx行動:示例和現實應用程序Apr 17, 2025 am 12:18 AM

NGINX可用於提升網站性能、安全性和可擴展性。 1)作為反向代理和負載均衡器,NGINX可優化後端服務和分擔流量。 2)通過事件驅動和異步架構,NGINX高效處理高並發連接。 3)配置文件允許靈活定義規則,如靜態文件服務和負載均衡。 4)優化建議包括啟用Gzip壓縮、使用緩存和調整worker進程。

NGINX單元:支持不同的編程語言NGINX單元:支持不同的編程語言Apr 16, 2025 am 12:15 AM

NGINXUnit支持多種編程語言,通過模塊化設計實現。 1.加載語言模塊:根據配置文件加載相應模塊。 2.應用啟動:調用語言運行時執行應用代碼。 3.請求處理:將請求轉發給應用實例。 4.響應返回:將處理後的響應返回給客戶端。

在Nginx和Apache之間進行選擇:適合您的需求在Nginx和Apache之間進行選擇:適合您的需求Apr 15, 2025 am 12:04 AM

NGINX和Apache各有優劣,適合不同場景。 1.NGINX適合高並發和低資源消耗場景。 2.Apache適合需要復雜配置和豐富模塊的場景。通過比較它們的核心特性、性能差異和最佳實踐,可以幫助你選擇最適合需求的服務器軟件。

nginx怎麼啟動nginx怎麼啟動Apr 14, 2025 pm 01:06 PM

問題:如何啟動 Nginx?答案:安裝 Nginx啟動 Nginx驗證 Nginx 是否已啟動探索其他啟動選項自動啟動 Nginx

怎麼查看nginx是否啟動怎麼查看nginx是否啟動Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

nginx怎麼關閉nginx怎麼關閉Apr 14, 2025 pm 01:00 PM

要關閉 Nginx 服務,請按以下步驟操作:確定安裝類型:Red Hat/CentOS(systemctl status nginx)或 Debian/Ubuntu(service nginx status)停止服務:Red Hat/CentOS(systemctl stop nginx)或 Debian/Ubuntu(service nginx stop)禁用自動啟動(可選):Red Hat/CentOS(systemctl disable nginx)或 Debian/Ubuntu(syst

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。