下載Nginx
到官網(wǎng)下載源碼文件,地址:http://nginx.org/en/download.html,選擇最新版本。本人下載的地址為:http://nginx.org/download/nginx-1.10.2.tar.gz,可用wget命令下載,也可以在windows系統(tǒng)上下載好再傳到linux上。
卸載httpd
如果系統(tǒng)默認(rèn)安裝了httpd服務(wù),卸載之。不卸載也沒關(guān)系,這里只是方便默認(rèn)80端口的處理。
yum -y remove httpd
解壓
tar -xzvf nginx-xxxxxx.tar.gz
安裝編譯器和依賴庫
yum install gcc gcc-c++ zlib-devel pcre-devel openssl-devel openssl-libs openssl -y
如果已經(jīng)安裝,就不必了
安裝前配置
cd命令轉(zhuǎn)到解壓后的目錄下。
./configure --prefix=/usr/local/nginx
這樣安裝時(shí)系統(tǒng)就會(huì)把Nginx安裝到/usr/local/nginx目錄下。
編譯
make
安裝
make install
安裝完成,接下來配置環(huán)境變量以后就不用使用絕對路徑來操作Nginx了:
vim /etc/profile.d/http.sh
加入以下內(nèi)容:
export PATH=/usr/local/nginx/sbin:$PATH
生效配置:
source !$
啟動(dòng)Nginx
nginx
nginx -s 后跟stop、reload來關(guān)閉和重載nginx,直接運(yùn)行nginx則啟動(dòng)服務(wù)。 如果啟動(dòng)時(shí)提示端口被占用,則需要找出被占用的進(jìn)程,或者更改/usr/local/nginx/conf/nginx.conf文件里的偵聽端口。
訪問Nginx
在瀏覽器上輸入 http://ip:port 如果出現(xiàn)“Welcome to nginx!”字樣,則證明安裝成功。如果訪問不了,先確認(rèn)防火墻是否禁止相應(yīng)端口了。
負(fù)載平衡配置示例
#user nobody;worker_processes 2;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid;events { accept_mutex on; #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on multi_accept on; #設(shè)置一個(gè)進(jìn)程是否同時(shí)接受多個(gè)網(wǎng)絡(luò)連接,默認(rèn)為off worker_connections 1024;#最大連接數(shù)}http { include mime.types;#文件擴(kuò)展名與文件類型映射表,此映射表主要用于部署在本nginx上的靜態(tài)資源 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;#連接超時(shí)時(shí)間 gzip on; #反向代理 #【配置1】此配置是[配置4]和[配置5]的結(jié)合 #此配置將請求轉(zhuǎn)發(fā)到兩個(gè)WEB服務(wù)器,根據(jù)客戶端IP分配目標(biāo)主機(jī),同時(shí)按權(quán)重分配流量 upstream app1 { ip_hash; server 192.168.14.132:8080 weight=5; server 192.168.14.133:80 weight=3; } #【配置2】 #默認(rèn)負(fù)載平衡配置,nginx應(yīng)用HTTP負(fù)載平衡來分發(fā)請求。 #upstream app1 { # server 192.168.14.132:8080; # server 192.168.14.133:80; #} #【配置3】 #最小連接負(fù)載平衡配置,nginx將盡量不使用繁忙的服務(wù)器,而是將新請求分發(fā)給不太忙的服務(wù)器。 #upstream app1 { # least_conn; # server 192.168.14.132:8080; # server 192.168.14.133:80; #} #【配置4】 #會(huì)話持久性配置,使用ip-hash,客戶端的IP地址用作散列密鑰, #以確定應(yīng)為客戶端請求選擇服務(wù)器組中的哪個(gè)服務(wù)器。 #此方法確保來自同一客戶端的請求將始終定向到同一服務(wù)器,除非此服務(wù)器不可用。 #upstream app1 { # ip_hash; # server 192.168.14.132:8080; # server 192.168.14.133:80; #} #【配置5】 #加權(quán)負(fù)載平衡配置,通過使用服務(wù)器權(quán)重進(jìn)一步影響nginx負(fù)載平衡算法。 #未配置權(quán)重的服務(wù)器,意味著所有指定的服務(wù)器被視為對特定負(fù)載平衡方法同等資格。 #upstream app1 { # ip_hash; # server 192.168.14.132:8080 weight=3; # server 192.168.14.133:80 weight=2; # server 192.168.14.134:80; # server 192.168.14.135:80; #} server {#可配置多個(gè)server以監(jiān)聽不同IP和不同端口 listen 80;#監(jiān)聽的端口 server_name localhost;#監(jiān)聽的服務(wù)器 #charset koi8-r; #access_log logs/host.access.log main; #反斜桿代表所有連接,此配置目的是將所有連接交給名為app1的upstream代理,實(shí)現(xiàn)負(fù)載平衡 location / { proxy_pass http://app1; } #圖片文件路徑,一般來說,靜態(tài)文件會(huì)部署在本機(jī)以加快響應(yīng)速度 #可配置多個(gè)這樣的location,滿足各種需求 location ~/.(gif|jpg|png)$ { root /home/root/images; } location ~/.(iso|zip|txt|doc|docx)$ { root /home/root/files; } #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; } # FastCGI是CGI全稱是“公共網(wǎng)關(guān)接口”(Common Gateway Interface) #對于我來說,使用Tomcat代替即可,請忽略此配置。 #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; #} # 添加黑名單,禁止某某訪問特定文件 # 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; # } #}}
配置完后,記得執(zhí)行以下命令生效配置
nginx -s reload
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選