1:Nginx的相關概念知識:css
1.1:反向代理:html
反向代理(Reverse Proxy)方式是指以代理服務器來接受internet上的鏈接請求,而後將請求轉發給內部網絡上的服務器,並將從服務器上獲得的結果返回給internet上請求鏈接的客戶端,此時代理服務器對外就表現爲一個服務器。java
1.2:負載均衡:nginx
負載均衡,英文名稱爲Load Balance,是指創建在現有網絡結構之上,並提供了一種廉價有效透明的方法擴展網絡設備和服務器的帶寬、增長吞吐量、增強網絡數據處理能力、提升網絡的靈活性和可用性。其原理就是數據流量分攤到多個服務器上執行,減輕每臺服務器的壓力,多臺服務器共同完成工做任務,從而提升了數據的吞吐量。apache
2:Nginx的安裝操做:vim
Nginx的官網:http://nginx.org/瀏覽器
2.1:將下載好的Nginx上傳到虛擬機上面,而後進行解壓縮操做,上傳過程省略,請自行腦補:tomcat
[root@master package]# tar -zxvf nginx-1.8.1.tar.gz -C /home/hadoop/
bash
2.2:編譯Ngnix源碼目錄:服務器
進入Ngnix源碼目錄:[root@master hadoop]# cd /home/hadoop/nginx-1.8.1/
檢查安裝環境,並指定未來要安裝的路徑:
#缺包報錯
checking for OS
+ Linux 2.6.32-696.10.1.el6.i686 i686
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for O_PATH ... not found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for SO_SETFIB ... not found
checking for SO_ACCEPTFILTER ... not found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... not found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 4 bytes
checking for long long size ... 8 bytes
checking for void * size ... 4 bytes
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 4 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 4 bytes
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
而後安裝一下缺乏的包:
[root@master nginx-1.8.1]# yum -y install gcc pcre-devel openssl openssl-devel
解決完錯誤之後再次執行,檢查安裝環境,並指定未來要安裝的路徑:
[root@master nginx-1.8.1]# ./configure --prefix=/home/hadoop/nginx
[root@master nginx-1.8.1]# ./configure --prefix=/home/hadoop/nginx
checking for OS
+ Linux 2.6.32-696.10.1.el6.i686 i686
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
checking for gcc -pipe switch ... found
checking for gcc builtin atomic operations ... found
checking for C99 variadic macros ... found
checking for gcc variadic macros ... found
checking for unistd.h ... found
checking for inttypes.h ... found
checking for limits.h ... found
checking for sys/filio.h ... not found
checking for sys/param.h ... found
checking for sys/mount.h ... found
checking for sys/statvfs.h ... found
checking for crypt.h ... found
checking for Linux specific features
checking for epoll ... found
checking for EPOLLRDHUP ... found
checking for O_PATH ... not found
checking for sendfile() ... found
checking for sendfile64() ... found
checking for sys/prctl.h ... found
checking for prctl(PR_SET_DUMPABLE) ... found
checking for sched_setaffinity() ... found
checking for crypt_r() ... found
checking for sys/vfs.h ... found
checking for nobody group ... found
checking for poll() ... found
checking for /dev/poll ... not found
checking for kqueue ... not found
checking for crypt() ... not found
checking for crypt() in libcrypt ... found
checking for F_READAHEAD ... not found
checking for posix_fadvise() ... found
checking for O_DIRECT ... found
checking for F_NOCACHE ... not found
checking for directio() ... not found
checking for statfs() ... found
checking for statvfs() ... found
checking for dlopen() ... not found
checking for dlopen() in libdl ... found
checking for sched_yield() ... found
checking for SO_SETFIB ... not found
checking for SO_ACCEPTFILTER ... not found
checking for TCP_DEFER_ACCEPT ... found
checking for TCP_KEEPIDLE ... found
checking for TCP_FASTOPEN ... not found
checking for TCP_INFO ... found
checking for accept4() ... found
checking for eventfd() ... found
checking for int size ... 4 bytes
checking for long size ... 4 bytes
checking for long long size ... 8 bytes
checking for void * size ... 4 bytes
checking for uint64_t ... found
checking for sig_atomic_t ... found
checking for sig_atomic_t size ... 4 bytes
checking for socklen_t ... found
checking for in_addr_t ... found
checking for in_port_t ... found
checking for rlim_t ... found
checking for uintptr_t ... uintptr_t found
checking for system byte ordering ... little endian
checking for size_t size ... 4 bytes
checking for off_t size ... 8 bytes
checking for time_t size ... 4 bytes
checking for setproctitle() ... not found
checking for pread() ... found
checking for pwrite() ... found
checking for sys_nerr ... found
checking for localtime_r() ... found
checking for posix_memalign() ... found
checking for memalign() ... found
checking for mmap(MAP_ANON|MAP_SHARED) ... found
checking for mmap("/dev/zero", MAP_SHARED) ... found
checking for System V shared memory ... found
checking for POSIX semaphores ... not found
checking for POSIX semaphores in libpthread ... found
checking for struct msghdr.msg_control ... found
checking for ioctl(FIONBIO) ... found
checking for struct tm.tm_gmtoff ... found
checking for struct dirent.d_namlen ... not found
checking for struct dirent.d_type ... found
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found
checking for openat(), fstatat() ... found
checking for getaddrinfo() ... found
checking for PCRE library ... found
checking for PCRE JIT support ... not found
checking for md5 in system md library ... not found
checking for md5 in system md5 library ... not found
checking for md5 in system OpenSSL crypto library ... found
checking for sha1 in system md library ... not found
checking for sha1 in system OpenSSL crypto library ... found
checking for zlib library ... found
creating objs/Makefile
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/home/hadoop/nginx"
nginx binary file: "/home/hadoop/nginx/sbin/nginx"
nginx configuration prefix: "/home/hadoop/nginx/conf"
nginx configuration file: "/home/hadoop/nginx/conf/nginx.conf"
nginx pid file: "/home/hadoop/nginx/logs/nginx.pid"
nginx error log file: "/home/hadoop/nginx/logs/error.log"
nginx http access log file: "/home/hadoop/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
2.3:編譯安裝(make是編譯,make install是安裝):
[root@master hadoop]# make && make install安裝不是一路順風的,開始將make && make install寫成了make && made install,確定沒有安裝成功了,而後我再執行make && make install就出現下面的狀況了,而後我從新./configure --prefix=/usr/local/nginx檢查安裝環境,並指定未來要安裝的路徑,最後再make && made install,貌似正常編譯,安裝了,雖然我也不是很清楚,這裏貼一下吧先,安裝好之後能夠測試是否正常:
[root@master hadoop]# make && make install
make: *** No targets specified and no makefile found. Stop.
[root@master hadoop]# make install
make: *** No rule to make target `install'. Stop.
[root@master hadoop]# make && make install
make: *** No targets specified and no makefile found. Stop.
[root@master hadoop]# ./configure --prefix=/home/hadoop/nginx
bash: ./configure: No such file or directory
[root@master hadoop]# cd /home/hadoop/nginx-1.8.1/
[root@master nginx-1.8.1]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src
[root@master nginx-1.8.1]# ./configure --prefix=/home/hadoop/nginx
2.4:安裝好之後測試是否正常:
安裝好之後指定的目錄會生成一些文件,如個人/home/hadoop/nginx目錄下面:
啓動Nginx的命令:[root@master sbin]# ./nginx
查看端口是否有ngnix進程監聽:[root@master sbin]# netstat -ntlp | grep 80
3:配置Nginx:
3.1:配置反向代理:
修改Nginx配置文件:
[root@master conf]# cd /home/hadoop/nginx/conf/
[root@master conf]# vim nginx.conf
server { listen 80; server_name master; #nginx所在服務器的主機名稱 #charset koi8-r; #access_log logs/host.access.log main; #反向代理的配置 location / { #攔截全部請求 root html; #index index.html index.htm; #這裏是代理走向的目標服務器:tomcat proxy_pass http://192.168.199.130:8080; }
具體配置以下所示:
下面貼圖這句話後面proxy_pass http://192.168.199.130:8080;
少了一個分號致使後面啓動nginx的時候出現錯誤:
本身都操點心就能夠了:
[root@master sbin]# ./nginx
nginx: [emerg] unexpected "}" in /home/hadoop/nginx/conf/nginx.conf:48
4:安裝Tomcat,將下載好的tomcat安裝包上傳到虛擬機,過程省略,而後解壓縮操做:
[root@slaver1 package]# tar -zxvf apache-tomcat-7.0.68.tar.gz -C /home/hadoop/
解壓縮好之後啓動Tomcat:
而後沒啓動起來,貌似說個人jdk沒有配置啥的,如今配置一下,配置過程省略,大概如上傳壓縮包,解壓縮,而後配置環境變量:
vim /etc/profile配置好之後使其當即生效:source /etc/profile,最後檢查一下是否安裝成功:java/javac/java -version
而後啓動tomcat,以下所示:
啓動好,能夠檢查一下是否啓動成功:
瀏覽器輸入本身的http://192.168.199.131:8080/
若是沒法訪問,多是防火牆的緣由:service iptables stop關閉防火牆;service iptables status查看防火牆是否關閉成功;
5:如今體現Nginx的功能了,我在master節點安裝的Nginx,而後在slaver1節點安裝的tomcat:
而後訪問master節點,會跳轉到slaver1的tomcat頁面:
http://192.168.199.130/本身的master節點的名稱;
6:Nginx的動靜分離:
動態資源 index.jsp
location ~ .*\.(jsp|do|action)$ {
proxy_pass http://ip地址:8080;
}
靜態資源:
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
expires 3d;
}
負載均衡:
在http這個節下面配置一個叫upstream的,後面的名字能夠隨意取,可是要和location下的proxy_pass http://後的保持一致。
http {
是在http裏面的, 已有http, 不是在server裏,在server外面
upstream tomcats {
server 192.168.199.130:8080 weight=1;#weight表示多少個
server 192.168.199.131:8080 weight=1;
server 192.168.199.132:8080 weight=1;
}
#卸載server裏
#~表明是大小寫敏感,.表明是任何非回車字符,*表明多個。
location ~ .*\.(jsp|do|action) {
proxy_pass http://tomcats; #tomcats是後面的tomcat服務器組的邏輯組號
}
}
待續......