当前位置:   article > 正文

手把手教你Mac M1简单快速配置Nginx

手把手教你Mac M1简单快速配置Nginx

教你Mac M1简单快速配置Nginx

相信很多入手Mac产品的小伙伴们都有一个头疼的问题!如何配置/安装程序员日常使用的软件/插件呢?今天小编就带大家了解如何快速安装配置好Nginx。

1、了解Nginx

  1. 是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。

  2. 常用于做请求转发、负载均衡、动静分离的一款反向代理服务器。

  3. Nginx以事件驱动的方式编写,所以有非常好的性能,同时也是一个非常高效的反向代理、负载平衡服务器。在性能上,Nginx占用很少的系统资源,能支持更多的并发连接,达到更高的访问效率;在功能上,Nginx是优秀的代理服务器和负载均衡服务器;在安装配置上,Nginx安装简单、配置灵活。

  4. Nginx支持热部署,启动速度特别快,还可以在不间断服务的情况下对软件版本或配置进行升级,即使运行数月也无需重新启动。

  5. 在微服务的体系之下,Nginx正在被越来越多的项目采用作为网关来使用,配合Lua做限流、熔断等控制。

  6. 对于Nginx的初学者可能不太容易理解web服务器究竟能做什么,特别是之前用过Apache服务器的,以为Nginx可以直接处理php、java,实际上并不能。对于大多数使用者来说,Nginx只是一个静态文件服务器或者http请求转发器,它可以把静态文件的请求直接返回静态文件资源,把动态文件的请求转发给后台的处理程序,例如php-fpm、apache、tomcat、jetty等,这些后台服务,即使没有nginx的情况下也是可以直接访问的。

2、安装 Homebrew

  1. 首先 homebrew是什么?它是Mac中的一款软件包管理工具,通过homebrew可以很方便的在Mac中安装软件或者是卸载软件。不了解的同学看以看官网[https://brew.idayer.com/guide/start/],然后在我们命令行中复制如下命令:
		/bin/bash -c "$(curl -fsSL https://cdn.jsdelivr.net/gh/ineo6/homebrew-install/install.sh)"
  • 1
  1. 如果最后安装完成后出现==> Installation successful! 就说明安装成功了。

  2. 通过 brew update 命令进行homebrew更新。

  3. 配置环境变量操作如下
    从macOS Catalina(10.15.x) 版开始,Mac 使用 zsh 作为默认 Shell ,使用 .zprofile。M1 款的 MacBook 肯定是比这个版本高的, 所以对应命令:

    	echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
    	eval "$(/opt/homebrew/bin/brew shellenv)" 
    
    • 1
    • 2
  4. 设置镜像「本文设置为中科大镜像」

		git -C "$(brew --repo)" remote set-url origin https://mirrors.ustc.edu.cn/brew.git
  • 1

3、安装 Nginx

  1. 我们使用homebrew进行Nginx安装操作,Nginx安装命令如下:
	brew install nginx
  • 1
  1. 查看Nginx配置信息:
	brew info nginx
  • 1

在这里插入图片描述

  1. 启动/重启Nginx命令:
    启动Nginx命令:brew services start nginx
    重启Nginx命令: brew services restart nginx

  2. 启动完Nginx后,通过在网页输入 http://localhost:8080/ 「Nginx默认端口号8080」
    在这里插入图片描述

  3. 如果小伙伴需要通过Nginx来配置请求转发则需要修改配置文件nginx.conf
    通过终端命令进入到nginx.conf文件所在地:

		open /opt/homebrew/etc/nginx
  • 1

在这里插入图片描述

  1. 修改配置文件
#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       8000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            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;
    #    }
    #}
     	server {
             listen       9001;
             server_name  localhost;

     		location ~ /eduservice/ {
     			proxy_pass http://localhost:8001;
            }
                 location ~ /eduoss/ {
     			proxy_pass http://localhost:8002;
         }
     }
    include servers/*;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  1. 修改操作图「修改完后记得重启Nginx」:
    在这里插入图片描述
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/264504
推荐阅读
相关标签
  

闽ICP备14008679号