架構圖參考如下:
1.下載nginx
2.解壓nginx壓縮包.tar.gz
解壓后得到文件夾
進入解壓目錄查看
3.安裝gcc
[root@localhost nginx-1.10.3]# yum -y install gcc-c++
安裝最基礎的模塊
[root@localhost nginx-1.10.3]# yum -y install pcre-devel
安裝gzip
[root@localhost nginx-1.10.3]# yum -y install zlib-devel
以上都是在編譯安裝nginx時需要的一些環境
4.編輯nginx
[root@localhost nginx-1.10.3]# ./configure --prefix=/usr/local/nginx
--------{{備注:可選
yum -y install openssl openssl-devel
--prefix=/usr/local/nginx --conf-path=/usr/local/nginx/nginx.conf --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module
--------}}
[root@localhost nginx-1.10.3]# make
[root@localhost nginx-1.10.3]# make install
5.啟動nginx
查看nginx是否啟動
root用戶進程號1303的才是,nobody的可以刪除
6.重啟nginx
7.防火墻問題
Centos7默認使用的firewall作為防火墻
[root@localhost ~]# firewall-cmd --state 查看防火墻運行狀態
running
[root@localhost ~]# systemctl start firewalld.service 開啟防火墻
[root@localhost ~]# systemctl stop firewalld.service 關閉防火墻
[root@localhost ~]# systemctl disable firewalld.service 徹底關閉防火墻,開機不啟動
[root@localhost ~]# systemctl enable firewalld.service 開啟防火墻,開機啟動
啟動防火墻后增加80端口
[root@localhost ~]# firewall-cmd --add-port=80/tcp
success
局域網訪問http://192.168.210.100
8.配置nginx,根據不同的域名轉發到不同的主機,直接編輯nginx.conf
路由器配置虛擬機服務器,將80端口指向nginx,也就是192.168.210.100這臺主機,可將配置文件自帶的server注釋掉,然后添加如下內容,這樣在訪問www.a.com這個域名時便可轉向到192.168.210.230這臺主機,在訪問www.b.com這個域名時轉向到192.168.210.231這臺主機
upstream a-server{
ip_hash;
server 192.168.210.230;
}
server {
listen 80;
server_name www.a.com;
location / {
proxy_pass http://a-server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream b-server{
ip_hash;
server 192.168.210.231;
}
server {
listen 80;
server_name www.b.com;
location / {
proxy_pass http://b-server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
修改完配置文件后記得重啟nginx.
重啟1:
進入nginx目錄
[root@localhost ~]# cd /usr/local/nginx/sbin/
[root@localhost sbin]# ls
nginx
[root@localhost sbin]# ./nginx -s reload
重啟2:
當nginx已經在運行時,則需要平滑重啟,步驟如下:
先查看nginx master的pid: ps aux|grep nginx|grep master
[root@localhost ~]# ps aux|grep nginx|grep master
root 1303 0.0 0.0 21932 1868 ? Ss 3月08 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nginx的pid為1303,如下平滑重啟命令,完成重啟
[root@localhost ~]# kill -HUP 1303
開放80端口:
sudo firewall-cmd --permanent --zone=public --add-port=80/tcp
sudo firewall-cmd --permanent --zone=public --add-port=80/udp
讓firewall-cmd重新加載配置:
sudo firewall-cmd --reload
再次列一下當前防火墻配置看是否開放成功:
firewall-cmd --list-all
遇到的錯誤及解決辦法
1.nginx/logs/nginx.pid" failed (2: No such file or directory)
解決方法:
重新編譯(make,make install),安裝就好.
2.[emerg]mkdir()"/var/temp/nginx/client" failed(2:No such file or directory)
解決方法:
查看了一下是由于沒有Nginx/client的目錄.缺少對應的文件,建立相應的文件就好.
參考1:https://www.cnblogs.com/ghjbk/p/6744131.html
參考2:http://blog.csdn.net/agangdi/article/details/41087921
參考3:負載均衡
upstream myserver{
# ip_hash;
server 192.168.200.21;
server 192.168.200.22;
server 192.168.200.23;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ .*\.(gif|jpg|png|htm|html|css|js|flv|ico|swf|eot|woff2|ttf|svg)(.*) {
proxy_pass http://myserver ;
proxy_redirect off;
proxy_set_header Host $host;
proxy_cache_valid 200 302 1h;
proxy_cache_valid 301 1d;
proxy_cache_valid any 1m;
expires 30d;
}
location / {
proxy_pass http://myserverbcs;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
Nginx的負載分發策略
Nginx 的 upstream目前支持的分配算法:
1.輪詢 ——1:1 輪流處理請求(默認)
每個請求按時間順序逐一分配到不同的應用服務器,如果應用服務器down掉,自動剔除,剩下的繼續輪詢。
2.權重 ——you can you up
通過配置權重,指定輪詢幾率,權重和訪問比率成正比,用于應用服務器性能不均的情況。
3.ip_哈希算法
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個應用服務器,可以解決session共享的問題。