nginx安裝(centos和ubuntu)

centos安裝nginx

安裝依賴庫

yum install gcc-c++
yum install pcre pcre-devel
yum install zlib zlib-devel
yum install openssl openssl-devel

安裝nginx

./configure --prefix=/usr/local/nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module(安裝ssl模塊)
make
make install

ubuntu安裝nginx

安裝依賴庫

安裝gcc g++的依賴庫

apt-get install build-essential
apt-get install libtool

安裝 pcre依賴庫

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev

安裝 zlib依賴庫

apt-get install zlib1g-dev

安裝 ssl依賴庫

apt-get install openssl

安裝nginx

./configure --prefix=/usr/local/nginx
make
sudo make install

常用命令

啓動nginx

./nginx

關閉nginx

./nginx -s stop
./nginx -s quit

重啓nginx

./nginx -s reopen

重新加載配置

./nginx -s reload

查看版本

./nginx -v

配置

在conf/nginx.conf中配置


#user  nobody;#指定nginx worker進程運行用戶以及用戶組,默認nobody賬號運行
worker_processes  1;#nginx要開啓的子進程數量,運行過程中監控每個進程消耗內存(一般幾M~幾十M不等)根據實際情況進行調整,通常數量是CPU內核數量的整數倍

#error_log  logs/error.log; #定義錯誤日誌文件的位置及輸出級別【debug / info / notice / warn / error / crit】 
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;#進程pid存放位置

#nginx工作模式配置
events {
    worker_connections  1024;#單個後臺worker process進程的最大併發鏈接數
}

#http模塊
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;#激活tcp_nopush參數可以允許把httpresponse header和文件的開始放在一個文件裏發佈。積極的作用是減少網絡報文段的數量

    #keepalive_timeout  0;#連接超時時間,單位是秒
    keepalive_timeout  65;

    #gzip  on;#開啓gzip壓縮功能

	#server模塊
	#基於域名的虛擬主機
    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;
    #    }
    #}

}

負載均衡測試

建立springboot的web項目testnginx:

在這裏插入圖片描述

在這裏插入圖片描述

打成jar包,上傳到Linux,放在3個文件夾下

在這裏插入圖片描述

通過啓動參數指定端口號–server.port=8081等

在這裏插入圖片描述

啓動3個程序
在這裏插入圖片描述

用瀏覽器訪問
在這裏插入圖片描述

配置負載均衡

http{
	……
	#配置負載均衡服務器列表
	upstream localweb{
		server localhost:8081;
		server localhost:8082;
		server localhost:8083;
	}
	server {
		listen       80;
		server_name  192.168.1.101;
		 location / {
		 	proxy_pass http://localweb;#請求轉向localweb定義的服務器列表
		 	#以下是一些反向代理的配置(可選擇性配置)
		 	#proxy_redirect off;
		 	proxy_set_header Host $host;#$host是指請求主機頭字段,否則爲服務器名稱。
		 	proxy_set_header X-Real-IP $remote_addr;#$remote_addr是指客戶端的IP地址
		 	#後端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
		 	proxy_set_header X-Forwarded-For $remote_addr;
		 	proxy_connect_timeout 90;          #nginx跟後端服務器連接超時時間(代理連接超時)
		 	proxy_send_timeout 90;             #後端服務器數據回傳時間(代理髮送超時)
		 	proxy_read_timeout 90;             #連接成功後,後端服務器響應時間(代理接收超時)
		 	proxy_buffer_size 4k;              #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
		 	proxy_buffers 4 32k;               #proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設置
		 	proxy_busy_buffers_size 64k;       #高負荷下緩衝大小(proxy_buffers*2)
		 	proxy_temp_file_write_size 64k;    #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
		 	client_max_body_size 10m;          #允許客戶端請求的最大單文件字節數
		 	client_body_buffer_size 128k;      #緩衝區代理緩衝用戶端請求的最大字節數
		 }
	}
	……
}

訪問http://192.168.1.101/go
在這裏插入圖片描述

可以看出,默認負載均衡算法是輪詢。
或者配置爲其他算法

	upstream localweb{
		ip_hash; #每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個後端服務器,可以解決session的問題。
		server localhost:8081;
		server localhost:8082;
		server localhost:8083;
	}
	upstream localweb{
	#weigth參數表示權值,權值越高被分配到的機率越大
		server localhost:8081 weight=5;
		server localhost:8082 weight=2;
		server localhost:8083 weight=1;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章