上一篇博文給大家分享了關于正向代理和反向代理的一些基本概念,而反向代理是我們開發過程中經常用到的,比如用反向代理伺服器為我們的服務端進行負載均衡、限流等。
- 分布式篇 - 一篇搞懂正向代理和反向代理
Nginx是什麼?
Nginx (
engine x
) 是一款輕量級、高性能的HTTP和反向代理
web
伺服器,同時也提供了IMAP/POP3/SMTP服務。其特點是占有記憶體少,并發能力強,事實上Nginx的并發能力在同類型的伺服器中表現較好。
安裝依賴環境
安裝gcc環境
yum install -y gcc-c++
使用
-y
選項,系統就不會詢問一些問題,像是否允許為該應用添加環境變量(
yes or no
),系統會預設你對這些問題的回答都是
yes
。
-y, --assumeyes answer yes for all questions
安裝prce庫
prce
庫,用于解析正規表達式。
yum install -y pcre pcre-devel
安裝壓縮和解壓縮依賴
zlib
是通用的壓縮庫,提供了一套
in-memory
壓縮和解壓函數。
yum install -y zlib zlib-devel
安裝openssl
SSL是安全套接層協定,用于HTTP安全傳輸,也就是HTTPS。
yum install -y openssl openssl-devel
下載下傳Nginx
Nginx下載下傳位址:
- Nginx
下載下傳穩定版(Linux系統)。
下載下傳好後,使用Xftp将該壓縮檔案放到虛拟機或伺服器中。
解壓Nginx
[root@localhost /]# cd /usr/local
[root@localhost local]# ll
總用量 1016
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
-rw-r--r--. 1 root root 1039530 1月 7 16:08 nginx-1.18.0.tar.gz
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 1月 7 15:44 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
[root@localhost local]# tar -zxvf nginx-1.18.0.tar.gz
編譯Nginx
[root@localhost local]# cd nginx-1.18.0
[root@localhost nginx-1.18.0]# ll
總用量 764
drwxr-xr-x. 6 1001 1001 4096 1月 7 16:24 auto
-rw-r--r--. 1 1001 1001 302863 4月 21 2020 CHANGES
-rw-r--r--. 1 1001 1001 462213 4月 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 1月 7 16:24 conf
-rwxr-xr-x. 1 1001 1001 2502 4月 21 2020 configure
drwxr-xr-x. 4 1001 1001 72 1月 7 16:24 contrib
drwxr-xr-x. 2 1001 1001 40 1月 7 16:24 html
-rw-r--r--. 1 1001 1001 1397 4月 21 2020 LICENSE
drwxr-xr-x. 2 1001 1001 21 1月 7 16:24 man
-rw-r--r--. 1 1001 1001 49 4月 21 2020 README
drwxr-xr-x. 9 1001 1001 91 1月 7 16:24 src
先進行配置,直接使用預設配置。
[root@localhost nginx-1.18.0]# ./configure
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
...
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"
配置的目的是為了建立
Makefile
檔案,為後面的編譯階段提供編譯檔案。
[root@localhost nginx-1.18.0]# ll
總用量 768
drwxr-xr-x. 6 1001 1001 4096 1月 7 16:24 auto
-rw-r--r--. 1 1001 1001 302863 4月 21 2020 CHANGES
-rw-r--r--. 1 1001 1001 462213 4月 21 2020 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 1月 7 16:24 conf
-rwxr-xr-x. 1 1001 1001 2502 4月 21 2020 configure
drwxr-xr-x. 4 1001 1001 72 1月 7 16:24 contrib
drwxr-xr-x. 2 1001 1001 40 1月 7 16:24 html
-rw-r--r--. 1 1001 1001 1397 4月 21 2020 LICENSE
-rw-r--r--. 1 root root 376 1月 7 16:37 Makefile
drwxr-xr-x. 2 1001 1001 21 1月 7 16:24 man
drwxr-xr-x. 3 root root 174 1月 7 16:41 objs
-rw-r--r--. 1 1001 1001 49 4月 21 2020 README
drwxr-xr-x. 9 1001 1001 91 1月 7 16:24 src
編譯
[root@localhost nginx-1.18.0]# make
安裝
[root@localhost nginx-1.18.0]# make install
啟動Nginx
[root@localhost nginx-1.18.0]# cd /usr/local/nginx
[root@localhost nginx]# ll
總用量 4
drwxr-xr-x. 2 root root 4096 1月 7 16:41 conf
drwxr-xr-x. 2 root root 40 1月 7 16:41 html
drwxr-xr-x. 2 root root 6 1月 7 16:41 logs
drwxr-xr-x. 2 root root 19 1月 7 16:41 sbin
[root@localhost nginx]# cd sbin
[root@localhost sbin]# ll
總用量 3764
-rwxr-xr-x. 1 root root 3851608 1月 7 16:41 nginx
./nginx 啟動nginx。
./nginx -t 檢查nginx的配置檔案是否符合要求
./nginx -s stop 此方式相當于先查出nginx程序id,再使用kill指令強制殺掉程序。
./nginx -s quit 此方式是待nginx程序處理完任務後,再停止nginx。
./nginx -s reload 重新開機nginx。
[root@localhost sbin]# ./nginx
關閉虛拟機防火牆,不然通路不了
nginx
服務。
[root@localhost sbin]# systemctl stop firewalld
[root@localhost sbin]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
1月 07 16:02:16 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
1月 07 16:02:17 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
1月 07 16:02:18 localhost.localdomain firewalld[688]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
1月 07 16:50:46 localhost.localdomain systemd[1]: Stopping firewalld - dynamic firewall daemon...
1月 07 16:50:47 localhost.localdomain systemd[1]: Stopped firewalld - dynamic firewall daemon.
使用虛拟機IP位址通路
nginx
服務。
[root@localhost sbin]# cd /usr/local
[root@localhost local]# ll
總用量 1016
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 11 root root 151 1月 7 16:48 nginx
drwxr-xr-x. 9 1001 1001 186 1月 7 16:37 nginx-1.18.0
-rw-r--r--. 1 root root 1039530 1月 7 16:08 nginx-1.18.0.tar.gz
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 1月 7 15:44 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src
[root@localhost local]# rm nginx-1.18.0.tar.gz
rm:是否删除普通檔案 "nginx-1.18.0.tar.gz"?y
[root@localhost local]# ll
總用量 0
drwxr-xr-x. 2 root root 6 4月 11 2018 bin
drwxr-xr-x. 2 root root 6 4月 11 2018 etc
drwxr-xr-x. 2 root root 6 4月 11 2018 games
drwxr-xr-x. 2 root root 6 4月 11 2018 include
drwxr-xr-x. 2 root root 6 4月 11 2018 lib
drwxr-xr-x. 2 root root 6 4月 11 2018 lib64
drwxr-xr-x. 2 root root 6 4月 11 2018 libexec
drwxr-xr-x. 11 root root 151 1月 7 16:48 nginx
drwxr-xr-x. 9 1001 1001 186 1月 7 16:37 nginx-1.18.0
drwxr-xr-x. 2 root root 6 4月 11 2018 sbin
drwxr-xr-x. 5 root root 49 1月 7 15:44 share
drwxr-xr-x. 2 root root 6 4月 11 2018 src