简单的对Ngxin学习(mac OS)

161 阅读3分钟

安装

mac可以直接通过homebrew对nginx进行安装,我们运行一下命令

// 安装nginx
brew install nginx
// 验证nginx是否安装成功
brew info nginx

运行

我们可以通过brew info nginx查询到安装目录,输入后可以看到大致如下内容

image.png 通过指令我们可以知道我们的安装路径以及配置文件路径

  • 安装路径: /opt/homebrew/Cellar/nginx/1.21.6_1
  • 配置文件路径: /opt/homebrew/etc/nginx/nginx.conf

同时我们可以通过nginx指令或者直接点击安装路径下/bin/nginx文件启动nginx服务,此时我们可以在浏览器中输入http://localhost:8080即可打开应用看到

image.png

关闭

首先我们有两种方式来查看对应的PID,分别可以通过

ps -ef|grep nginx

查询特定进程的状态信息,或者是根据端口进行查询比如

lsof -i:8080

他们能得到以下信息

image.png

其中我们知道对应的PID后,我们方可通过kill命令杀死对应的进程,比如上图,只需要输入如下即可

kill 18293

即可,可能大家会比较疑惑为什么关闭的是18293而不是18294,这是因为ngxin的多进程架构的关系,在启动时会启动两个进程一个是master process, 另外一个是woker process的子进程。 如果我们关闭的是worker进程,我们重新查询进程,会发现又回出现一个新的子进程。

配置

我们打开对应的配置文件,它本身的基础配置信息如下


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

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

静态资源服务器

大多数情况下,nginx会被当前成一个静态资源服务器进行使用,我们看到默认配置中实际上已经帮我们启动了一个静态资源服务器,我们单独把这一块的配置取出来

    server {
        listen       8080;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

解读起来其实也比较简单,大致意思如下:

  1. 启用服务,使用8080端口
  2. 服务名称localhost
  3. 如果访问/, 那就以html为目录为根目录,访问对应文件,其中设置index.html为首页
  4. 如果发生了500 502 503 504的错误,就转发到/50x.html;
  5. 可以从本地html查询/50x.html文件

那么基本上一个静态资源服务就只需要这些相关的配置项目就可以了,如果想要部署,只需要调整对应的 root 参数即可

完成配置后可以通过nginx -s reload命令直接更新配置,不用重启

代理服务

前端开发在开发应用的时候遇到的另外一个比较常见的问题就是如何解决跨域问题,其中使用nginx进行代理,就是一个很好的方法,那我们应该如何进行配置呢?

server {
    listen       8080;
    server_name  myServe;

    location /some-path {
      proxy_pass  https://**.**.cn/some-path/;
    }
 }

其中关键点就在于proxy_pass配置项,它类似于root配置,只不过是填写对应的转发地址就好

总结

前端对于nginx的使用,以上两个功能已经可以覆盖我们使用过程中的绝大部分场景,当然除去以上两个功能nginx还有很多很强大的能力,比如负载均衡,FastCGI等等。如果感兴趣的同学可以通过官方的教程自行学习