Mac系统下Nginx的安装和配置

1367人浏览 / 0人评论 / 添加收藏

简介

Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器 [13] ,同时也提供了IMAP/POP3/SMTP服务。

Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,公开版本1.19.6发布于2020年12月15日。 [11]其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。2022年01月25日,nginx 1.21.6发布。

[12]Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好。

目录

1、Homebrew 安装

2、官网下载安装
一 、Homebrew 安装

1、更新 Homebrew
brew update

2、下载并安装 Nginx
brew install nginx

3、查看 nginx 配置信息
brew info nginx

4、配置内容描述 (brew info nginx)
zhanghua@Breeze ~ % brew info nginx
// 版本信息
==> nginx: stable 1.25.1 (bottled), HEAD
HTTP(S) server and reverse proxy, and IMAP/POP3 proxy server

// Nginx安装目录
https://nginx.org/
/usr/local/Cellar/nginx/1.25.1_1 (26 files, 2.4MB) *
 Poured from bottle using the formulae.brew.sh API on 2023-08-10 at 10:48:47

// 安装来源
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/nginx.rb
License: BSD-2-Clause
==> Dependencies
Required: openssl@3 ✔, pcre2 ✔
==> Options
--HEAD
   Install HEAD version

// 根目录
==> Caveats
Docroot is: /usr/local/var/www

// 重点!!!nginx 的配置文件及默认启动端口 8080
The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

// Nginx将在 Server 目录下加载所有文件,在这个
nginx will load all files in /usr/local/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:
 /usr/local/opt/nginx/bin/nginx -g daemon off;
==> Analytics
install: 11,885 (30 days), 57,023 (90 days), 84,746 (365 days)
install-on-request: 11,855 (30 days), 56,930 (90 days), 84,620 (365 days)
build-error: 3 (30 days)

5、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
#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 {

   #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;


   upstream xx{
         // 负载均衡配置
        server 127.0.0.1:8080 weight =1;
        server 127.0.0.1:8081 weight =1;
   }

   // nginx 端口 ,只要访问8080端口 就会被nginx 监听 ,如果电脑端口有冲突 可以更换端口号
   server {
       listen       8080;
       server_name  localhost;

       #charset koi8-r;

       #access_log  logs/host.access.log  main;

       location / {
           root   html;
           index  index.html index.htm;
           // 反向代理
           proxy_pass http://域名
       }

       #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;

   #证书 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时建议备份,防止修改错误,注意添加的代码均要是 ; 结尾。

 

三、mac本地配置nginx

1. 以管理员权限进入到 nginx配置文件中

sudo vim /usr/local/etc/nginx/nginx.conf

2.在serve同级下,复制serve内容

server{
  listen 80;
  server_name test.com;
 location / {
   proxy_pass http://127.0.0.1:3000;
 }
}

点击键盘上的  i(编辑)

3.退出保存

点击 esc退出,shift+:   进入输入命令下

输入  wq(保存退出)

4. sudo nginx  启动nginx

5. 一定要配置本地文件

5.1. sudo vim /etc/hosts

5.2. 输入i进入编辑,

添加  127.0.0.1   nginx中配置的域名
 

全部评论