Nginx單機環境搭建及配置

簡介

Nginx是一個web服務器也可以用來做負載均衡及反向代理使用,目前使用最多的就是負載均衡,詳細見百度

第一步:下載

下載地址:http://nginx.org/en/download.html,我選擇的版本是nginx-1.10.3(如果版本不一致可能存在差異)。

下載下來壓縮包爲nginx-1.10.3.tar.gz。請使用tar -xzvf nginx-1.10.3.tar.gz解壓

第二步:安裝

進入Nginx解壓目錄,執行以下命令安裝:
./configure && make && make install

默認安裝目錄爲:/usr/local/nginx

第三步:配置

爲了使得配置的server更加清晰,此處將每一個server單獨剝離出來,場景如下:

有兩臺應用服務器,IP+PORT分別爲:10.100.96.142:8080和10.100.96.143:8080,需配置輪詢負載均衡

  1. 在/usr/local/nginx/conf下創建vhost目錄,新建8080.xml(名字隨意)文件
  2. 8080.xml配置如下:

    server {
      listen 80;
      server_name 8080;
    
      #access_log logs/host.access.log main;
    
      location /ssm {
        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 ~ .*$ {
        proxy_pass http://8080;
      }
    }
  3. 配置/usr/local/nginx目錄下的nginx.xml(注意upstream的配置,有需要可以加權重weight):

    #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;
    
      upstream 8080 {
        server 10.100.96.142:8080;
        server 10.100.96.143:8080;
      }
    
      include vhost/*.conf;
    }

第四步:啓動Nginx

  1. 啓動前檢查,如果配置錯誤會提示
    ./sbin/nginx -t
  2. 重啓Nginx服務
    查找當前nginx進程號,然後輸入命令:./sbin/nginx -s reload或kill -HUP 進程號 實現重啓nginx服務
  3. 啓動完成後,輸入Nginx地址,如下即爲成功

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章