Mac: Nginx 的安装和配置

本地安装Nginx步骤:

1)安装Homebrew
终端输入
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install))"
大概过个二十分钟左右安装完成,其间提示输入密码,输入Mac密码,安装成功后就可以安装nginx了。
2)安装nginx
终端输入

brew install nginx 

3)验证结果
安装好了,就可以启动nginx了,终端输入

brew services start nginx
  • 启动成功,可以直接转到浏览器输入:
    http://localhost:8080
    nginx.png
  • 重启:
 brew services restart nginx

查看Nginx配置文件步骤:

安装完以后,可以在终端输出的信息里看到一些配置路径:

/usr/local/etc/nginx/nginx.conf (配置文件路径)

/usr/local/var/www (服务器默认路径)

/usr/local/Cellar/nginx/1.13.10/1.17.10(1.17.10根据自己安装的版本而定)
Nginx配置文件主要分成四部分:

main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。

  • main部分设置的指令将影响其它所有部分的设置;
  • server部分的指令主要用于指定虚拟主机域名、IP和端口;
  • upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;
  • location部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。
    备注:
    他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。
    对于这四个部分涉及到字段及包含的解释如下:


    nginx config.png

    1.全局块:开头到events,影响服务器运行的配置指令

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

备注:
1)worker_processes 1; #配置处理并发处理量
2)#pid logs/nginx.pid; #进程号

2.events块:服务器与用户网络连接

events {
    worker_connections  1024; #最大的连接数
}

3.http块:包括http块,server块。代理缓存日志等都在这一块配置

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;
    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/*;
}

参考文档

1)https://blog.csdn.net/weixin_47872288/article/details/118515340?share_token=49f013ca-5bd6-42b1-a927-e22769810f78
2)https://zh.wikipedia.org/wiki/Nginx

你可能感兴趣的:(Mac: Nginx 的安装和配置)