Linux下Nginx1.8安裝

 

安裝pcre

 

   wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz 
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make
make install

 

安裝openssl

 

wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz

cd openssl-1.0.1c/javascript

./config
make
make install

 

安裝zlib         

 

wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install

 

下載Nginx1.8至/usr/local,看命令吧:php

$ tar xzf nginx-1.8.0.tar.gz
$ ./configure --prefix=/home/nginx/nginx
$ make
$ make install

若出現cc找不到執行css

 

 yum -y install gcc automake autoconf libtool make

若啓動出現以下錯誤html

error while loading shared libraries: libpcre.so.1: cannot open shared object file:
No such file or directory

執行如下命令java

ln -s /usr/local/lib/libpcre.so.1 /lib64

安裝完成以後生成一個nginx的文件夾,cd進去並切換到sbin目錄node

啓動命令linux

$ ./nginx

關閉命令:nginx

$ ./nginx -s stop

優雅重啓後端

$ ./nginx -s reload

下面是配置:    瀏覽器

一、反向代理配置

修改部署目錄下conf子目錄的nginx.conf文件(如nginx-1.5.13\conf\nginx.conf)內容,可調整相關配置。

反向代理配置示例:

location / {
	#設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP
	 proxy_set_header Host $host;
	 proxy_set_header X-Real-IP $remote_addr;
	 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
	 #禁用緩存
	 proxy_buffering off;
	 #設置反向代理的地址
	 proxy_pass http://192.168.1.1;
}

代理地址根據實際狀況修改。

二、負載均衡配置

nginx 的 upstream默認是以輪詢的方式實現負載均衡,這種方式中,每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。

另一種方式是ip_hash:每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。

負載均衡配置示例:

upstream test{
	 #ip_hash;
	 server 192.168.1.251;
	 server 192.168.1.252;
	 server 192.168.1.247;
 }
server {
	listen       80;
	server_name  helloword;
	location / {
		 #反向代理的地址
		 proxy_pass http://test;     
	}
}

Upstream命名和服務器地址根據實際狀況修改。

三、負載均衡+反向代理完整配置示例

nginx.conf:
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream test{
		 #ip_hash;
		 server 192.168.1.251;
		 server 192.168.1.252;
		 server 192.168.1.247;
	 }
    server {
        listen       80;
        server_name  2;
        location / {
        #設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實IP
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
			 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             #禁用緩存
             proxy_buffering off;
             #反向代理的地址
             proxy_pass http://test;   
        }
    }
}

四、動靜分離配置    

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream test {
		 #ip_hash;
		 server 192.168.1.251;
		 server 192.168.1.252;
		 server 192.168.1.247;
	 }
    server {
        listen       80;
        server_name  2;
		#配置Nginx動靜分離,定義的靜態頁面直接從Nginx發佈目錄讀取。        
		location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { 
		  root /usr/local/nginx/html/myloan; 
		  #expires定義用戶瀏覽器緩存的時間爲7天,若是靜態頁面不常更新,能夠設置更長,這樣能夠節省帶寬和緩解服務器的壓力
		  expires      7d; 
        }
		 #全部jsp、do的動態請求都交給後面的tomcat處理 
         location ~ (\.jsp)|(\.do)$ { 
			  #tomcat地址
			  proxy_pass http://test;   
			  proxy_redirect off;  
			  proxy_set_header HOST $host;  
			  proxy_set_header X-Real-IP $remote_addr;  
			  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
			  client_max_body_size 10m;  
			  client_body_buffer_size 128k;  
			  proxy_connect_timeout 90;  
			  proxy_send_timeout 90;  
			  proxy_read_timeout 90;  
			  proxy_buffer_size 4k;  
			  proxy_buffers 4 32k;  
			  proxy_busy_buffers_size 64k;  
			  proxy_temp_file_write_size 64k;  
		}      
    }
}

附錄:nginx.conf配置詳解

#運行用戶
user www-data;    
#啓動進程,一般設置成和cpu的數量相等
worker_processes  1;

#全局錯誤日誌及PID文件
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

#工做模式及鏈接數上限
events {
    use   epoll;             #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能
    worker_connections  1024;#單個後臺worker process進程的最大併發連接數
    # multi_accept on; 
}

#設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
     #設定mime類型,類型由mime.type文件定義
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    #設定日誌格式
    access_log    /var/log/nginx/access.log;

    #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
    #必須設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,下降系統的uptime.
    sendfile        on;
    #tcp_nopush     on;

    #鏈接超時時間
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;
    
    #開啓gzip壓縮
    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #設定請求緩衝
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

    #設定負載均衡的服務器列表
     upstream mysvr {
    #weigth參數表示權值,權值越高被分配到的概率越大
    #本機上的Squid開啓3128端口
    server 192.168.8.1:3128 weight=5;
    server 192.168.8.2:80  weight=1;
    server 192.168.8.3:80  weight=6;
    }


   server {
    #偵聽80端口
        listen       80;
        #定義使用www.xx.com訪問
        server_name  www.xx.com;

        #設定本虛擬主機的訪問日誌
        access_log  logs/www.xx.com.access.log  main;

    #默認請求
    location / {
          root   /root;      #定義服務器的默認網站根目錄位置
          index index.php index.html index.htm;   #定義首頁索引文件的名稱

          fastcgi_pass  www.xx.com;
         fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name; 
          include /etc/nginx/fastcgi_params;
        }

    # 定義錯誤提示頁面
    error_page   500 502 503 504 /50x.html;  
        location = /50x.html {
        root   /root;
    }

    #靜態文件,nginx本身處理
    location ~ ^/(images|javascript|js|css|flash|media|static)/ {
        root /var/www/virtual/htdocs;
        #過時30天,靜態文件不怎麼更新,過時能夠設大一點,若是頻繁更新,則能夠設置得小一點。
        expires 30d;
    }
    #PHP 腳本請求所有轉發到 FastCGI處理. 使用FastCGI默認配置.
    location ~ \.php$ {
        root /root;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name;
        include fastcgi_params;
    }
    #設定查看Nginx狀態的地址
    location /NginxStatus {
        stub_status            on;
        access_log              on;
        auth_basic              "NginxStatus";
        auth_basic_user_file  conf/htpasswd;
    }
    #禁止訪問 .htxxx 文件
    location ~ /\.ht {
        deny all;
    }
     
     }
}
相關文章
相關標籤/搜索