关于nginx:代理是什么nginx快速入门反向代理hexo个人博客

3次阅读

共计 4764 个字符,预计需要花费 12 分钟才能阅读完成。

@TOC

前言


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

    本文阐明 请大家务必查看


本文有两个版本,具体版、简洁版

前者适宜老手,后者适宜新手(不便大家查找,从而过滤掉某些步骤,节约工夫老本) 所以大家按需查看哟。

具体版 简洁版

简洁版:蕴含所有步骤,以及命令的执行过程(适宜老手)

简洁版:只蕴含命令(适宜有肯定熟练度的人)

工作原理


正向代理

  • 客户端 —> 代理服务器 —> 拜访的域名 —> 拜访的服务器
  • 客户端 <— 代理服务器 <— 拜访的域名 <— 拜访的服务器
    简略总结:正向代理是到客户端

举个栗子:咱们打韩服的 LOL 有提早,咱们就能够找一个代理(香港的vpn),代理拜访国外的服务器,而后返回给代理,最初返回给咱们。能够了解成加速器。

反向代理

  • 客户端 —> 拜访的域名 —> 代理服务器 —> 拜访的服务器
  • 客户端 <— 拜访的域名 <— 代理服务器 <— 拜访的服务器
    简略总结:反向代理是到服务端

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

环境筹备


零碎 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 如图所示:

配置 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如下图所示:

卸载

[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**
正文完
 0