【Mac】nginx 安装与配置

本文详细介绍了在Mac上安装和配置nginx的过程,包括基本配置、代理服务器设置、域名映射、SSL/TLS证书的自签名创建及配置,以及配置优化案例。通过实例指导如何启动和关闭nginx,并提供了相关参考链接。
摘要由CSDN通过智能技术生成

目录

一. 问题

  最近弄了些东西,想放服务器上,次哦,远程存储库不存在,算了,自己本地先搞个瞅瞅;
  这就很尴尬了,电脑上没有。

二. 安装

首先输入如下命令,查找 nginx 一下,看有没有稳定版本,

$ brew search nginx

==> Formulae
nginx ✔

发现只有一个选项,没得选,然后进行安装,命令如下:

$ brew install nginx

安装过程如下:

...
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/manifests/2021-10-26
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/ca-certificates/blobs/sha256:1bbd45c16a0b9912174c553a6d7ae1b67b11abbeb3155eaf03109bb62d8e5381
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:1bbd45c16a0b9912174c553a6d7ae1b67b11abbeb3155eaf03109bb62d8e5381?se=2021-12-
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/manifests/1.1.1l_1
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/openssl/1.1/blobs/sha256:9a14367813591b51e30828c7d86499479bc6201954f6b10ed591b40cd3b71cc1
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:9a14367813591b51e30828c7d86499479bc6201954f6b10ed591b40cd3b71cc1?se=2021-12-
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pcre/manifests/8.45
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/pcre/blobs/sha256:5e5cc7a5bf8bb6488ec57d4263bf6b0bc89e93252a0a2460f846de29373162d8
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:5e5cc7a5bf8bb6488ec57d4263bf6b0bc89e93252a0a2460f846de29373162d8?se=2021-12-
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/manifests/1.21.4
######################################################################## 100.0%
==> Downloading https://ghcr.io/v2/homebrew/core/nginx/blobs/sha256:1705176bc483a5fe2dfaa0872a370f6b7d05f2e3283a49c444276ad72673a71e
==> Downloading from https://pkg-containers.githubusercontent.com/ghcr1/blobs/sha256:1705176bc483a5fe2dfaa0872a370f6b7d05f2e3283a49c444276ad72673a71e?se=2021-12-
######################################################################## 100.0%
==> Installing nginx 1.21.4 

==> Installing dependencies for nginx: ca-certificates, openssl@1.1 and pcre
==> Installing nginx dependency: ca-certificates
==> Pouring ca-certificates--2021-10-26.all.bottle.tar.gz
==> Regenerating CA certificate bundle from keychain, this may take a while...
🍺  /usr/local/Cellar/ca-certificates/2021-10-26: 3 files, 208.5KB
==> Installing nginx dependency: openssl@1.1
==> Pouring openssl@1.1--1.1.1l_1.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/openssl@1.1/1.1.1l_1: 8,073 files, 18.5MB
==> Installing nginx dependency: pcre
==> Pouring pcre--8.45.monterey.bottle.tar.gz
🍺  /usr/local/Cellar/pcre/8.45: 204 files, 5.7MB
==> Installing nginx
==> Pouring nginx--1.21.4.monterey.bottle.tar.gz
==> Caveats
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To restart nginx:
  brew services restart nginx
Or, if you don't want/need a background service you can just run:
  /usr/local/opt/nginx/bin/nginx -g daemon off;
==> Summary
🍺  /usr/local/Cellar/nginx/1.21.4: 26 files, 2.2MB

三. 配置

3.1 nginx.conf

默认配置路径:

/usr/local/etc/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 {
   
    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"';
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    # log_format  main  '$status $body_bytes_sent "$http_referer" ';
    # log_format  main  '"$http_user_agent" "$http_x_forwarded_for"';

    # 日志: logs/access.log
    access_log  logs/access.log  main;

    sendfile        on;
    tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
   
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        location / {
   
            # 配置代理服务器 www.xxx.com
            proxy_pass http://www.xxx.com:8081;
        }

        # 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  anothe
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值