Nginx windows 版安装及使用(入门)

Nginx诞生的背景

  Nginx 和 Apache 都属于web 服务器,在Nginx 之前,Apache 以开源、稳定一直是世界上公认和用户最多的服务器。但Apache技术诞生比较久远,目前难以满足高并发的应用场景。Nginx 以轻量、高并发就应运而生了。

Nginx拥有的能力

反向代理

  • 正向代理
    •   譬如访问google网站,国内是访问不到的,此时可以找一个国外的代理服务器C,C可以访问google。 正向代理服务器C代理的是客户端的请求,转发给指定服务端。服务端不知道是哪个客户端发过来的请求,客户端和代理服务器在同一个LAN。
  • 反向代理
    •   目前互联网发展迅猛,单点服务远不能满足当下的服务请求。譬如国内的大型购物网站都是分布式部署,客户端发送购物请求,都会经过反向代理,均衡的转发客户端的请求到分布式的服务端。此时反向代理服务对客户端是透明的,即反向代理隐藏了服务端信息。客户端不知道访问的是后端N个服务中哪个服务的资源,反向代理服务器和后端服务器是同一个LAN

负载均衡

  • 权重轮询(默认)  
    •   轮询1..N台服务,其中有宕机的会主动剔除,按照权重分发请求,权重越高,分发的请求越多。
  • ip_hash
    •   经过hash算法,把指定IP和指定的服务端绑定,即指定IP的请求转发到指定的服务端。可以解决session共享的问题,保持有状态请求。
  • fair
    •   智能调度算法,处理时间长的分发的请求少,处理时间短的分发的请求多。Nginx默认不支持,需要安装upstream_fair模块。
  • url_hash
    •   和ip_hash原理一致,只是把指定url和指定服务器绑定。

动静分离

  针对静态资源,如图片、js、css等交由Nginx处理,而动态请求交由后端服务器来处理。根据请求url来做区分。

WEB缓存

  Nginx可以对不同的文件做不同的缓存处理,配置灵活,并且支持FastCGI_Cache,主要用于对FastCGI的动态程序进行缓存。配合着第三方的ngx_cache_purge,对制定的URL缓存内容可以的进行增删管理

Windows版本安装

官网地址

安装

  •   和其他windows的app一样,一路next 。我安装在D:\nginx-1.16.0

基本使用

Nginx 的文件结构

...              #全局块

events {         #events块
   ...
}

http      #http块
{
    ...   #http全局块
    server        #server块
    { 
        ...       #server全局块
        location [PATTERN]   #location块
        {
            ...
        }
        location [PATTERN] 
        {
            ...
        }
    }
    server
    {
      ...
    }
    ...     #http全局块
}
  • 1、全局块:配置影响nginx全局的指令。一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
  • 2、events块:配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。
  • 3、http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。
  • 4、server块:配置虚拟主机的相关参数,一个http中可以有多个server。
  • 5、location块:配置请求的路由,以及各种页面的处理情况。

版本1.16 默认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       80;
        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;
    #    }
    #}

}
View Code

加上注释

#user  nobody;                      ## 配置用户或者组,例如root用户
worker_processes  1;                ## 进程数,默认开启1个就够了。调优选择:CPU核心数

error_log  logs/error.log debug;    ##    指定日志的存放路径和级别
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;            ##    nginx运行启动后就会生成这个标示文件,记录 nginx主进程的 ID 号


events {
    accept_mutex on;                   ## 设置网路连接序列化,防止惊群现象发生,默认为on
    multi_accept on;                  ## 设置一个进程是否同时接受多个网络连接,默认为off
    #use epoll;                      ## 事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
    worker_connections  1024;        ## 最大连接数,默认为512, 最大连接数 = worker_processes * worker_connections/4
}


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;                    ## 允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
    #tcp_nopush     on;                    ## 开启或者关闭nginx在FreeBSD上使用TCP_NOPUSH套接字选项, 在Linux上使用TCP_CORK套接字选项。 
                                            ## 选项仅在使用sendfile的时候才开启。 开启此选项允许
                                            ## 在Linux和FreeBSD 4.*上将响应头和正文的开始部分一起发送

    #keepalive_timeout  0;                
    keepalive_timeout  65;                ## 连接超时时间,可以在http,server,location块

    gzip  on;                            ## 网页、css、js等静态资源的大小会大大的减少,从而可以节约大量的带宽,
                                            ## 提高传输效率,给用户快的体验

    server {
        listen       8080;                                    ## 监听端口
        server_name  localhost;                                ## 监听地址

        #charset koi8-r;                                    ##    

        access_log  logs/host.access.log  main;        

        location / {                                        ## 请求的url过滤,按照精准匹配 - 普通匹配 - 正则匹配的优先级别
            root   html;                                    ## 根目录
            index  index.html index.htm;                    ## 这是默认页
            proxy_pass  http://localhost:8888/gis-service/swagger-ui.html;                  ## 请求转发baidu 定义的服务器列表
            deny 192.168.60.175;                              ## 拒绝的ip
            allow 127.0.0.1;                                 ## 允许的ip
        }

        #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;                        ## https 的端口号
    #    server_name  localhost;                        ## 服务地址
    #
    #    ssl_certificate      cert.pem;                ## ssl证书文件位置(常见证书文件格式为:crt/pem)
    #    ssl_certificate_key  cert.key;                ## ssl证书key位置
    #
    #    ssl_session_cache    shared:SSL:1m;            ## 设置存储session参数的缓存的类型和大小
    #    ssl_session_timeout  5m;                    ## session超时时间
    #
    #    ssl_ciphers  HIGH:!aNULL:!MD5;                ## 加密套件
    #    ssl_prefer_server_ciphers  on;
    #
    #    location / {                                ## 
    #        root   html;                            ## 根目录
    #        index  index.html index.htm;
    #    }
    #}

}
View Code

  配置OK后,启动nginx,并访问http://localhost:8080即可展示出nginx的欢迎页。

操作回溯

  • nginx -v              查看版本;发现报错nginx : 无法将“nginx”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。 因为我在power shell 运行的。后面在cmd中运行,结果就对了。
  • start nginx          启动nginx;cd 到nginx.exe 目录,运行此命令。cmd窗口会一闪而过。
  • nginx -s reload   优雅重启,并重新载入配置文件nginx.conf。

参考链接

  https://www.runoob.com/w3cnote/nginx-setup-intro.html

  https://blog.csdn.net/qq_26975307/article/details/89531000

 

posted @ 2020-03-25 17:21  正宗老菜鸟  阅读(1256)  评论(0编辑  收藏  举报