【代理是什么?】nginx快速入門+反向代理hexo個(gè)人博客
@
目錄
前言
-
什么是nginx?
Nginx是一款輕量級(jí)的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器。特點(diǎn)是占有內(nèi)存少,并發(fā)能力特別強(qiáng)。 -
nginx作用?
http代理,如:正向代理、反向代理
本文說明 請(qǐng)大家務(wù)必查看
本文有兩個(gè)版本,詳細(xì)版、簡(jiǎn)潔版。
前者適合新手,后者適合老手(方便大家查找,從而過濾掉某些步驟,節(jié)約時(shí)間成本) 所以大家按需查看喲。
| 詳細(xì)版 | 簡(jiǎn)潔版 |
|---|
簡(jiǎn)潔版:包含所有步驟,以及命令的執(zhí)行過程(適合新手)
簡(jiǎn)潔版:只包含命令(適合有一定熟練度的人)
工作原理
正向代理
- 客戶端--->代理服務(wù)器--->訪問的域名--->訪問的服務(wù)器
- 客戶端<---代理服務(wù)器<---訪問的域名<---訪問的服務(wù)器
簡(jiǎn)單總結(jié):正向代理是到客戶端
舉個(gè)栗子:我們打韓服的LOL有延遲,我們就可以找一個(gè)代理(香港的vpn),代理訪問國(guó)外的服務(wù)器,然后返回給代理,最后返回給我們。可以理解成加速器。

反向代理
- 客戶端--->訪問的域名--->代理服務(wù)器--->訪問的服務(wù)器
- 客戶端<---訪問的域名<---代理服務(wù)器<---訪問的服務(wù)器
簡(jiǎn)單總結(jié):反向代理是到服務(wù)端
舉個(gè)栗子:我們?cè)谠L問百度的時(shí)候一直都是www.baidu.com這個(gè)域名,其實(shí)域名后面有很多服務(wù)器(ip地址),訪問域名-->代理服務(wù)器后到百度的服務(wù)器,最后返回給我們html頁面。

環(huán)境準(zhǔn)備
| 系統(tǒng) | Vcpu | Memory | 網(wǎng)卡類型 |
|---|---|---|---|
| centos7 | 2 | 4 | NAT模式 |
沒有hexo博客環(huán)境看這里:還不會(huì)搭建博客嗎?centos7系統(tǒng)部署hexo博客新手入門-進(jìn)階,看這一篇就夠了_小葉的技術(shù)Logs的博客-CSDN博客
詳細(xì)版
入門:搭建步驟
配置阿里云epel源:
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo #下載epel源,否則沒有nginx包
--2022-04-18 21:54:35-- http://mirrors.aliyun.com/repo/epel-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 113.207.38.89, 113.207.38.90, 113.207.38.85, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|113.207.38.89|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 664 [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/epel.repo’
100%[======================================================================================>] 664 --.-K/s in 0s
2022-04-18 21:54:36 (131 MB/s) - ‘/etc/yum.repos.d/epel.repo’ saved [664/664]
yum安裝nginx:
[root@localhost ~]# yum install -y nginx #yum安裝nginx軟件
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 nginx.x86_64 1:1.20.1-9.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.20.1-9.el7 for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
啟動(dòng)nginx:
[root@localhost ~]# systemctl stop firewalld && systemctl disabel firewalld #關(guān)閉防火墻、開機(jī)不自啟防火墻
[root@localhost ~]# setenforce 0 #臨時(shí)關(guān)閉selinux
[root@localhost ~]# systemctl start nginx #啟動(dòng)nginx
[root@localhost ~]# systemctl enable nginx #開機(jī)自啟nginx
瀏覽器驗(yàn)證訪問nginx如圖所示:

配置default.conf文件
實(shí)現(xiàn)反向代理:
[root@localhost ~]# cat /etc/nginx/conf.d/default.conf # 如果沒有conf.d目錄需要?jiǎng)?chuàng)建,默認(rèn)我們這里下載的nginx版本沒有default.conf,創(chuàng)建即可
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://127.0.0.1:4000; #http根/目錄,代理到http://127.0.0.1:4000
}
}
[root@localhost ~]# systemctl restart nginx
最后驗(yàn)證
你會(huì)發(fā)現(xiàn)直接瀏覽器輸入ip,不輸入4000端口也實(shí)現(xiàn)了訪問
over如下圖所示:


卸載
[root@localhost ~]# yum remove -y nginx #yum卸載nginx
Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-9.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================================================================
Package Arch Version Repository Size
================================================================================================================================
Removing:
nginx x86_64 1:1.20.1-9.el7 @epel 1.7 M
Transaction Summary
================================================================================================================================
Remove 1 Package
Installed size: 1.7 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : 1:nginx-1.20.1-9.el7.x86_64 1/1
warning: /etc/nginx/nginx.conf saved as /etc/nginx/nginx.conf.rpmsave
Verifying : 1:nginx-1.20.1-9.el7.x86_64 1/1
Removed:
nginx.x86_64 1:1.20.1-9.el7
Complete!
[root@localhost ~]# rm -rf /etc/nginx* #刪除相關(guān)配置文件目錄
簡(jiǎn)潔版
搭建步驟
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y nginx
[root@localhost ~]# systemctl stop firewalld && systemctl disabel firewalld
瀏覽器 驗(yàn)證
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl enable nginx
[root@localhost ~]# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
# index index.html index.htm;
proxy_pass http://127.0.0.1:4000;
}
}
[root@localhost ~]# systemctl restart nginx
瀏覽器 驗(yàn)證
卸載
[root@localhost ~]*# yum remove -y nginx*
[root@localhost ~]*# rm -rf /etc/nginx**

什么是nginx?
Nginx是一款輕量級(jí)的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器。特點(diǎn)是占有內(nèi)存少,并發(fā)能力特別強(qiáng)
浙公網(wǎng)安備 33010602011771號(hào)