Nginx初識 Nginx如何分發多台服務器,多個網站


1.下載Nginx

下載地址:http://nginx.org/en/download.html

本文所選版本:nginx-1.4.7

2.下載后需要解壓,解壓后的nginx目錄如下:

 

 

 3.新建測試html頁,並命名為:“default.htm”

<html>
<head>
<title>nginx學習</title>
</head>
<body>
   Welcome to localhost:8001 port website !
<body>
</html>

4.本地IIS搭建4個網站。如下所示。每個網站的端口號相對應,比如nginx-6001,那么它的端口號就是6001,本地訪問:http://localhost:6001

 

 5.118服務器搭建1個網站。局域網服務器端口號為:6005,局域網訪問:http://192.168.0.118:6005或者localhost:6005

 

 6.228服務器搭建1個網站。局域網服務器端口號為:6006,局域網訪問:http://192.168.0.228:6006或者localhost:6006

7.每個網站的目錄下存放default.htm文件。並修改default.htm文件中的IP和端口號。請參考上面第3點

 

 8.找到nginx目錄下的conf中的nginx.conf文件,先復制一份副本,避免改錯原版。再用記事本打開方式進行編輯。

 

9.以下是nginx.conf文件,帶#號為前綴的表示注釋,#后面的單行字符將均為注釋內容,紅色部分表示為新增的功能。

【分發配置】

upstream name_erp 中upstream表示上游模塊,name_erp表示模塊配置的名稱,括號里包含了分發給目標服務器(這里即本機)訪問的ip和端口號,weight代表權重。你可以理解為負載百分比。

name_api表示另外一個模塊,模塊是api網站的分發。該模塊中包含了局域網其他主機電腦的ip和端口號

【監聽配置】

listen 表示監聽的端口號,這里是監聽8889端口號

listen下面的server_name 表示監聽的IP,這里是監聽localhost.

proxy_pass 表示指向反向代理的配置,這里指向了name_erp配置,並增加了前綴http,如果是https訪問則需要修改

監聽配置意在表示,將對本機localhost:8889請求時會進行監聽。屆時會分發到name_erp中的目標ip和端口號網站中。

#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;
 upstream name_erp{  #+add
   server localhost:6001 weight=2;#+add#weight代表權重,數據越大,分擔越多
      server localhost:6002 weight=1;#+add
      server localhost:6003 weight=1;#+add
      server localhost:6004 weight=1;#+add
 }#+add
    
  upstream name_api{  #+add
    server 192.168.63.118:6005 weight=1;#+add
    server 192.168.60.228:6006 weight=1;#+add
 }#+add

    server {
 listen 8888;#+up-old=80
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
         proxy_pass    http://name_erp;#+add 志向指定的服務器集群(一定要加,否則頁面一直指向歡迎頁,不知道指向那個頁面)
        }

        #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
    # 另一個虛擬主機混合使用基於IP、名稱和端口的配置
    #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;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
 # } #} #HTTPS 服務器 
    server {   listen 8889;
        server_name  localhost;
      location / {
          root   html;
          index  index.html index.htm;
          proxy_pass    http://name_api;#+add    } }
}

 


 在最后面再增加一個監聽Https服務器的配置。內容與上方大致一致。 

這里指定的proxy_pass指向了name_api。

監聽端口為8888,監聽IP為localhost.

監聽配置意在表示,將對本機localhost:8888請求時會進行監聽。屆時會分發到name_api中的目標ip和端口號網站中。

注意此處的監聽端口號不能與上方監聽的端口號一致,避免沖突。

 

 

 

 

 10.安裝路徑:這里把nginx相關文件放在英文目錄下,中文目錄下不會生效。

 

11.運行cmd,啟動Nginx,並輸入以下命令:

定位到nginx目錄:

cd 存放nginx的英文根目錄路徑

啟動nginx:

start nginx

 

 

 

12.nginx命令大全:

殺掉所有Nginx進程並啟動

taskkill /IM  nginx.exe  /F

start nginx

 

 


免責聲明!

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



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