Mac下安装Nginx及使用

Mac 安装 Nginx 及配置

  1. 更新 Homebrew

    brew update
    
  2. 下载并安装 Nginx

    brew install nginx
    
  3. 查看 nginx 配置信息

    tong@MacBook-Pro ~ % brew info nginx
    
    版本信息
    ==> nginx: stable 1.25.1 (bottled), HEAD
    HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server
    https://nginx.org/
    
    Nginx安装目录
    /opt/homebrew/Cellar/nginx/1.25.1 (26 files, 2.4MB) *
    Poured from bottle using the formulae.brew.sh API on 2023-06-17 at 15:52:25
    
    安装来源
    From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb
    License: BSD-2-Clause
    ==> Dependencies
    Required: [email protected] ✔, pcre2 ✔
    ==> Options
    --HEAD
    Install HEAD version
    
    根目录
    ==> Caveats
    Docroot is: /opt/homebrew/var/www
    
    
    重点!!!nginx 的配置文件及默认启动端口 8080
    The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that
    nginx can run without sudo.
    
    Nginx将在 Server 目录下加载所有文件,在这个
    nginx will load all files in /opt/homebrew/etc/nginx/servers/.
    
    To start nginx now and restart at login:
    brew services start nginx
    Or, if you don't want/need a background service you can just run:
    /opt/homebrew/opt/nginx/bin/nginx -g daemon off;
    ==> Analytics
    install: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days)
    install-on-request: 81,387 (30 days), 151,458 (90 days), 151,458 (365 days)
    build-error: 0 (30 days)
    
  4. Nginx常用命令

    启动
    brew services start nginx
    
    停止
    brew services stop nginx
    
    终端进入nginx 目录
    cd /opt/homebrew/etc/nginx
    
    编辑 nginx.conf 配置文件
    vim nginx.conf 
    
    编辑完成后:wq保存退出
    
    重新加载配置文件
    nginx -s reload
    
  5. Nginx配置文件nginx.conf

    全局配置
    
    events {
        worker_connections  1024;
    }
    
    
    http {
    
        http 配置
        
        #原本没有,自己手动添加
        upstream XX{
        	//负载均衡配置
        }
        
        #主要配置 server,配自己需要的信息
        server {
            listen       8070;
            server_name  localhost;
    
           
            location / {        		
                root   html;
                index  index.html index.htm;
                proxy_pass http://localhost:8080;
            }
    
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
        }
    
    
    
    		//原注释,现在释放生效
        # HTTPS server
        
        server {
            listen       443 ssl;
            server_name  localhost;
    				
    		#证书 cert.pen和 cert.key换成证书的目录
            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/*;
    }
    
    

    配置文件主要是配置 server 里面的内容,需要理解反向代理的概念,在编辑 nginx.conf时建议备份,防止修改错误,注意添加的代码均要是 结尾。

你可能感兴趣的:(nginx,macos,运维)