Mac 安装和使用Nginx教程

  1. 安装
brew install nginx
  1. 查看 nginx 版本
nginx -v

查看Nginx信息

brew info nginx

在这里插入图片描述

  1. 启动 nginx
nginx
  1. 查看 nginx 是否启动成功
    在浏览器中访问http://localhost:8080,如果出现如下界面,则说明启动成功.
    在这里插入图片描述

  2. 关闭nginx

nginx -s stop

也可以使用下面的命令启动,但是配置文件nginx.conf修改后用这个命令执行不生效,故不建议使用:sudo brew services stop nginx

  1. 重新加载nginx
nginx -s reload
  1. 查看Nginx配置文件
vim /usr/local/etc/nginx/nginx.conf

查看Nginx配置文件的位置以及是否配置成功 nginx -t 命令
在这里插入图片描述
8. 为nginx指定一个配置文件

nginx -c filename 为nginx指定一个配置文件

Nginx配置文件内容如下:


#user  nobody;
#-- Nginx占用几个CPU
worker_processes  1; 

#-- 记录错误日志,默认只记录错误日志,如果想记录警告、信息等内容,可以在后面添加notice、info
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
	#-- 每个CPU支持1024个并发。总处理数量:worker_processes * worker_connections
    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; 如果开启gzip,则文件传输会按照chuncked形式传输,并且不会response content-length字段,因为被压缩了,不知道实际大小。

    server {
        listen       8080;
        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;
    #    }
    #}
    include servers/*;
}

log_format 各个变量值的含义:

log_format main   '$remote_addr - $remote_user [$time_local] '
	                '"$request" $status $bytes_sent '
	                  '"$http_j_forwarded_for" "$http_x_forwarded_for" '
	                   '"$http_referer" "$http_user_agent" '
	                   '"$gzip_ratio"';
  • $remote_addr
  • $remote_user[$time_local]
  • $bytes_sent
  1. 查看nginx安装目录, 如下命令:
open /usr/local/etc/nginx/

在这里插入图片描述
在这里插入图片描述

总结nginx常见的配置:

nginx的配置文件路径:/usr/local/etc/nginx/nginx.conf
nginx的服务器默认路径:/usr/local/var/www
nginx的安装路径:/usr/local/Cellar/nginx/1.15.5


动静分离:动态资源和静态资源分开。
静态资源:HTML、CSS、JavaScript、图片、音频、视频等


如何启动Nginx缓存:

Nginx启用缓存需要在最顶层的http节点下配置proxy_cache_path命令。
链接:https://www.cnblogs.com/asheng2016/p/proxy_cache.html

proxy_cache_path  /data/nginx/cache  levels=1:2  keys_zone=STATIC_CACHE:10m  
                  inactive=24h  max_size=1g;

server {
    listen 80;

    proxy_cache       STATIC_CACHE;
    proxy_cache_valid 200 302 10m;
    proxy_cache_valid 301      1h;
    proxy_cache_valid any      1m;
    proxy_cache_use_stale  error timeout invalid_header updating
                           http_500 http_502 http_503 http_504;

    location ~* \.(jpg|jpeg|png|gif|bmp|mp4)$ {
        proxy_pass http://static;
        include    proxy_params;
    }
}

nginx配置文件解析

# 全局配置区
#user  nobody; Nginx启动使用的账户,不用管,Nginx在安装的时候会自动安装一个账号,也不需要你自己登陆
worker_processes  1; # 占用的CPU数量,也可以是auto,会根据当前系统CPU核数来匹配数量

#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; # 每一个worker进程可以处理的连接数(并发数)
}

events { # events 事件区,子进程核心配置

}

http { # http服务器配置区
	include       mime.types; # 支持的媒体文件类型
    default_type  application/octet-stream; # 如果在mime.types里没有找到,则调用默认的default_type类型,当前为二进制流的形式
    # 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;
    # error_log logs/error.log
	server { # 不同服务配置区
		location { # location 不同请求路径配置区
			# 具体配置选项
		}
	}
}


mail { # 邮件代理配置区
	server { # 邮件服务配置区
		# 具体配置选项
	}	
}

tip:将本地文件上传到远程服务器的工具:https://www.filezilla.cn/download/client

vim设置行号:进入vim模式后输入 set nu

待继续:链接


参考:https://www.zze.xyz/archives/nginx-proxycache.html
参考:https://www.cnblogs.com/redirect/p/10066766.html#21-proxy_cache_path
参考:https://www.cnblogs.com/asheng2016/p/proxy_cache.html

  • 0
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

. . . . .

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值