美文网首页
什么是nginx

什么是nginx

作者: 屎香味十足 | 来源:发表于2020-09-18 19:53 被阅读0次

简介

nginx是俄罗斯人Igor Sysoev编写的轻量级web服务器,它不仅仅是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP代理服务器;它以事件驱动的方式编写,有非常好的性能,同时也是一个高小的反向代理、负载均衡服务器,它占用很少的系统资源,能支持更多的并发连接,达到更高的访问效率,性能非常之高(高性能、高并发、低内存);安装简单,配置灵活,支持热部署,支持不断服务情况下对软件或者配置进行升级,运行数月也不用重启;

nginx只是一个景泰年文件服务器或者http请求转发器,并不能直接处理php与java,它可以把静态文件的请求直接返回静态文件资源,把动态文件的请求转发给后台处理程序;

安装

安装homebrew

mac下可以先安装homebrew,安装honebrew如下:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

出现以下log表示安装完成了

Installation successful!

安装nginx

使用homebrew安装nginx(如果homebrew有更新,会先更新homebrew后再进行安装,并且更新时间较久,请耐心等待)

brew install nginx

等待nginx安装完毕后,可以执行以下命令查看是否安装成功,与安装版本号

ngixn -v

// nginx version: nginx/1.19.2

在mac中,我们nginx的各个目录如下:

安装目录:/usr/local/Cellar/nginx
配置文件目录:/usr/local/etc/nginx
服务器默认路径:/usr/local/var/www

在nginx配置文件中,各个文件的作用如下:

启动nginx

默认启程程序是在/usr/local/Cellar/nginx/1.19.2/bin/中,因为全局已经 有nginx了,所以直接nginx也是可以启动的,后者到该目录下启动:

// 安装成功后,直接执行nginx 命令就可以启动了
nginx

// 或者到该目录下启动
cd /usr/local/Cellar/nginx/1.19.2/bin/
nginx

以下是默认的配置,默认的服务端口是8080,最后还给出了https的配置demo:

zhangchengdeMacBook-Pro:nginx zhangcheng$ cat 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"';

    #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后,打开:http://localhost:8080/

image.png

重载nginx配置

reload与restart不一样,reload后重新加载配置文件 ,服务不会终端;并且reload会测试config语法,出错会rollback上一次正确的配置文件;

但是restart会重启nginx服务,造成服务一瞬间的终中断,如果配置文件有问题,时间可能会比较长;如果是 要执行restart,并且是线上服务,最好做好配置文件备份;线上最好使用reload;

reload 只是重新加载配置文件,不会清理nginx 的一些缓存,在有些需要清理缓存的场景需要restart ,例如upstream 后端配置的集群服务地址是域名而不是ip,当后端IP 变了,就需要清除该域名的解析缓存,此时需要重启而不是reload。

nginx -s reload
image.png

快速停止nginx

第一次刷新就直接断掉

nginx -s stop

优雅停止nginx

先服务完已打开的链接,第一次刷新还是可以使用的,第二次刷新就断了

nginx -s quit

判断配置文件是否正确

当修改了配置文件后,可能存在配置文件出错,或者代理链接出错的情况,这边需要先校验以下配置修改是否正确

nginx -t
image.png

卸载nginx

brew uninstall nginx

相关文章

  • 贵安项目nginx配置

    关于nginx的使用以及为什么使用【Nginx】什么是Nginx?为什么使用Nginx?nginx 之 proxy...

  • nginx与uWsgi

    什么是nginx 为什么使用nginx nginx、WSGI、uwsgi、uWSGI nginx和uWSGI 的意...

  • 2018-05-30

    Nginx 部署 1.什么是nginx nginx 是俄罗...

  • Nginx搭建与应用

    主要内容: 1)为什么要使用Nginx?2)什么是Nginx?3)Nginx能做什么?4)Nginx安装5)配置文...

  • 什么是Nginx?

    什么是nginx? Nginx是一个http服务器(web服务器)。是一个使用c语言开发的高性能的http服务器及...

  • 什么是Nginx?

    最近在请教公司后端开发一些系统架构的问题,感觉有很多东西都是只是听过,但是其作用完全不了解,所以想通过这个专题,把...

  • 什么是nginx

    简介 nginx是俄罗斯人Igor Sysoev编写的轻量级web服务器,它不仅仅是一个高性能的HTTP和反向代理...

  • 什么是Nginx?

    借鉴博客:https://blog.csdn.net/kisscatforever/article/details...

  • nginx入门(四) nginx的简单实战

    前面讲了什么是nginx和nginx能干什么,如何在linux上安装nginx以及nginx配置文件字段含义。本文...

  • nginx入门指南

    Nginx入门指南 一.Nginx概述: 1.什么是Nginx? Nginx(发音同engine x)是一个网页服...

网友评论

      本文标题:什么是nginx

      本文链接:https://www.haomeiwen.com/subject/hutsyktx.html