Nginx初体验


之前没怎么用过代理服务器Nginx,主要也是因为没有架构知识,根本不会去部署相关的机器。但是最近公司内部在调试的时候,经常用本机去充当Ngnix代理服务器,由于对这块知识掌握得还不是很牢固,因此自发对它进行一个简单的扫盲学习。

主要参考 快速入门:大白话梳理Nginx,全网最通俗易懂 - 知乎 (zhihu.com)

定义

Nginx (engine x) 是一个高性能的 HTTP反向代理 web服务器

正向代理 & 反向代理

正向代理,就是客户端将自己的请求率先发给代理服务器,通过代理服务器将请求转发给服务器。我们常用的VPN就是一种代理服务器,为了可以连上国外的网站,客户端需要使用一个可以连接外网的服务器作为代理,并且客户端能够连接上该代理服务器。
在这里插入图片描述

反向代理与正向代理不同,正向代理是代理了客户端,而反向代理则是代理服务器端。在有多台服务器分布的情况下,为了能让客户端访问到的IP地址都为同一个网站,就需要使用反向代理。
在这里插入图片描述

从图中可以看出,比方说服务器-1的IP:port是localhost:8080,但是却可以反向代理后面三个服务器-2,服务器-3,服务器-4,至于选择哪个服务器和端口,有一个负载均衡策略在里面。

安装与尝试

直接 homebrew 在mac上安装ngnix,安装目录在 /opt/homebrew/etc/nginx,后面的一系列配置都在 nginx.conf,我们新建目录 myconf 来保存后面运行的各种目录,这里用之前老的nginx一直访问不了网页,准备直接删了重新下载了。

Bug1: HomeBrew出了点问题,brew search nginx说github凭证失效了,重新上这个网址https://github.com/settings/tokens/new?scopes=gist,repo,workflow&description=Homebrew配了个永久的token

Warning: Error searching on GitHub: GitHub API Error: Requires authentication
The GitHub credentials in the macOS keychain may be invalid.
Clear them with:
  printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
Create a GitHub personal access token:
https://github.com/settings/tokens/new?scopes=gist,repo,workflow&description=Homebrew
echo 'export HOMEBREW_GITHUB_API_TOKEN=your_token_here' >> ~/.zshrc

Bug2: install nginx找不到版本,那是因为homebrew目录树老了

brew install nginx
Error: nginx: unknown or unsupported macOS version: :dunno

brew update-reset 解决

Nginx卸载安装的命令如下:

brew uninstall nginx
brew install nginx
brew info nginx # 之前就是不会用这个命令查看各种配置,导致自己找来找去

原始网页都放在 /opt/homebrew/var/www/ 这个目录下面,当然也可以我们自己去配置网页

sudo nginx -c /opt/homebrew/etc/nginx/nginx1.conf # 根据配置文件启动
ps -ef|grep nginx # 启动完以后可以找到对应的线程去杀死

最终配置文件nginx1.conf如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
    		# 所有被反向代理的服务器都指向该端口
        listen       802;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
				# 匹配到/A路径的时候,就会进行反向代理
        location /A {
        		# 反向代理到/opt/homebrew/etc/nginx/mypage 下
            root   /opt/homebrew/etc/nginx/mypage;
            # 默认主页的网址,在上面root路径下
            index  index.html index.htm;
        }
	
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

上面注意,自定义location /A 虽然是根据/A完成了匹配,但是到root里面去查找对应的资源的时候,也需要走进/A这个路径,也就是在root目录/opt/homebrew/etc/nginx/mypage下还需要创建一个A目录,这才能既匹配又能够访问。在 /opt/homebrew/etc/nginx/mypage/A 里面放上一个index.html页面就行。

  • 20
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

互联网民工蒋大钊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值