Mac下安装Nginx及使用

时间:2025-03-18 14:21:23
  • 更新 Homebrew

    brew update
    
  • 下载并安装 Nginx

    brew install nginx
    
  • 查看 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
    /
    
    Nginx安装目录
    /opt/homebrew/Cellar/nginx/1.25.1 (26 files, 2.4MB) *
    Poured from bottle using the  API on 2023-06-17 at 15:52:25
    
    安装来源
    From: /Homebrew/homebrew-core/blob/HEAD/Formula/
    License: BSD-2-Clause
    ==> Dependencies
    Required: openssl@1.1 ✔, 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/ 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)
    
  • Nginx常用命令

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

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

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

  • 相关文章