【代理是什么?】nginx快速入门+反向代理hexo个人博客

@TOC

前言


  • 什么是nginx?
    Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。特点是占有内存少,并发能力特别强。

  • nginx作用?
    http代理,如:正向代理、反向代理

本文说明 请大家务必查看


本文有两个版本,详细版、简洁版

前者适合新手,后者适合老手(方便大家查找,从而过滤掉某些步骤,节约时间成本) 所以大家按需查看哟。

详细版 简洁版

简洁版:包含所有步骤,以及命令的执行过程(适合新手)

简洁版:只包含命令(适合有一定熟练度的人)

工作原理


正向代理

  • 客户端--->代理服务器--->访问的域名--->访问的服务器
  • 客户端<---代理服务器<---访问的域名<---访问的服务器
    简单总结:正向代理是到客户端

举个栗子:我们打韩服的LOL有延迟,我们就可以找一个代理(香港的vpn),代理访问国外的服务器,然后返回给代理,最后返回给我们。可以理解成加速器。

image.png

反向代理

  • 客户端--->访问的域名--->代理服务器--->访问的服务器
  • 客户端<---访问的域名<---代理服务器<---访问的服务器
    简单总结:反向代理是到服务端

举个栗子:我们在访问百度的时候一直都是www.baidu.com这个域名,其实域名后面有很多服务器(ip地址),访问域名-->代理服务器后到百度的服务器,最后返回给我们html页面。

image.png

环境准备


系统 Vcpu Memory 网卡类型
centos7 2 4 NAT模式

没有hexo博客环境看这里:还不会搭建博客吗?centos7系统部署hexo博客新手入门-进阶,看这一篇就够了_小叶的技术Logs的博客-CSDN博客

详细版

入门:搭建步骤

配置阿里云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

启动nginx:

[root@localhost ~]# systemctl stop firewalld && systemctl disabel firewalld  #关闭防火墙、开机不自启防火墙
[root@localhost ~]# setenforce 0  #临时关闭selinux
[root@localhost ~]# systemctl start nginx #启动nginx
[root@localhost ~]# systemctl enable nginx #开机自启nginx

浏览器验证访问nginx如图所示:

image.png

配置default.conf文件

实现反向代理:

[root@localhost ~]# cat /etc/nginx/conf.d/default.conf  # 如果没有conf.d目录需要创建,默认我们这里下载的nginx版本没有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;  #http根/目录,代理到http://127.0.0.1:4000
        }
}

[root@localhost ~]# systemctl restart nginx

最后验证
你会发现直接浏览器输入ip,不输入4000端口也实现了访问

over如下图所示:

image.png

image.png


卸载

[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*  #删除相关配置文件目录

简洁版

搭建步骤

 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
浏览器 验证

[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
浏览器 验证

卸载

[root@localhost ~]*# yum remove -y nginx*
[root@localhost ~]*# rm -rf /etc/nginx**
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 224,896评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,283评论 3 402
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 172,085评论 0 367
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 61,010评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,015评论 6 400
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,492评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,858评论 3 428
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,829评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,374评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,409评论 3 346
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,527评论 1 355
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,131评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,858评论 3 339
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,296评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,434评论 1 276
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,087评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,597评论 2 366

推荐阅读更多精彩内容