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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM