一、目標
負載均衡實現高性能web服務器
二、Nginx的介紹:
參考此鏈接
https://blog.csdn.net/weixin_42812527/article/details/106120808
三、服務器環境
服務器 | 角色 | 環境說明 |
---|---|---|
192.168.1.93 | 負載均衡服務器 | 系統:Ubuntu 16.04.4 |
192.168.1.98 | 開發服務器 | 系統:Ubuntu 16.04.4 |
192.168.1.99 | 開發服務器 | 系統:Ubuntu 16.04.6 |
四、Nginx的下載
4.1、第一種方式
sudo apt install nginx
4.2第二種方式
一、下載:
wget http://nginx.org/download/nginx-1.4.2.tar.gz #下載安裝包
tar -zxvf nginx-1.4.2.tar.gz #進行解壓
二、安裝:
./configure --prefix=/usr/local/nginx #配置安裝目錄
make && make install #進行安裝
注:安裝目錄爲/usr/local/nginx
三、安裝成功之後會顯示下邊四個文件夾
conf #配置文件所放的位置
html #html文件
logs #日誌文件
sbin #主要二進制程序
四、啓動
./sbin/nginx
五、查看是否啓動
ps -ef | grep nginx
成功:
root 64144 1 0 14:21 ? 00:00:00 nginx: master process nginx
xxx+ 64145 64144 0 14:21 ? 00:00:01 nginx: worker process
xxx+ 64146 64144 0 14:21 ? 00:00:01 nginx: worker process
xxx+ 64147 64144 0 14:21 ? 00:00:01 nginx: worker process
xxx+ 64148 64144 0 14:21 ? 00:00:01 nginx: worker process
五、修改配置文件
5.1、配置文件路徑
第一種方式下載 配置文件在:/etc/nginx/nginx.conf
第二種方式下載 配置文件在:/usr/local/nginx/nginx.conf
作者 採用第一種方式下載的,所以配置路徑在:/etc/nginx/nginx.conf
5.2、修改配置文件
user xugaopeng;
worker_processes 1; // 有1個工作的子進程,可以自己設置或者 auto
pid /run/nginx.pid;
events { # 一般是配置nginx連接的特性
worker_connections 1024; // 這是指 一個子進程最大允許連1024個連接,可以自行修改。
# multi_accept on;
}
http { // 服務器配置
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log; // 日誌文件
error_log /var/log/nginx/error.log; // 錯誤日誌文件
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
// 自己添加如下
upstream myproject {
server 192.168.1.98:9192 weight=3; // 加權輪詢。
server 192.168.1.99:9192;
}
server {
listen 8008;
server_name 192.168.1.93;
location / {
proxy_pass http://myproject;
}
}
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
// 注意、作者在這裏自己修改的內容 通過 // 註釋
六、開啓、關閉 Nginx
一、開啓
cd /etc/nginx
執行 nginx
二、關閉
cd /etc/nginx
執行 nginx -s stop
七、參考文章
https://www.cnblogs.com/xiugeng/p/10155283.html#_label0_3
https://www.nginx.cn/nginxchscommandline#command
https://www.nginx.cn/doc/example/loadbanlance.html
八、總結
作者這裏主要是配置了 Nginx 負載均衡,不是很難。基本上按照流程來就行了,如果遇到問題,可以自己百度。