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