国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > Nginx > 正文

CentOS6.5環境安裝nginx服務器及負載均衡配置操作詳解

2024-08-30 12:29:31
字體:
來源:轉載
供稿:網友

本文實例講述了CentOS6.5環境安裝nginx服務器及負載均衡配置操作。分享給大家供大家參考,具體如下:

1、下載PCRE, 是一個用C語言編寫的正則表達式函數庫

[root@localhost pcre-8.36]# cd /tmp/download/[root@localhost download]# wget http://nchc.dl.sourceforge.net/project/pcre/pcre/8.37/pcre-8.37.tar.gz[root@localhost download]# tar zxvf pcre-8.36.tar.gz

2、下載zlib庫

[root@localhost pcre-8.36]# cd /tmp/download/[root@localhost download]# wget http://ncu.dl.sourceforge.net/project/libpng/zlib/1.2.8/zlib-1.2.8.tar.gz[root@localhost download]# tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8

4、下載SSL

[root@localhost zlib-1.2.8]# cd ..[root@localhost download]# wget http://www.openssl.org/source/openssl-1.0.1p.tar.gz[root@localhost download]# cd openssl-1.0.1c[root@localhost openssl-1.0.1c]# tar -zxvf openssl-1.0.1c.tar.gz

5、下載nginx

[root@localhost download]# wget http://nginx.org/download/nginx-1.2.8.tar.gz[root@localhost download]# tar -zxvf nginx-1.2.8.tar.gz

6、安裝

[root@localhost download]mv pcre-8.36 /usr/local/[root@localhost download]mv zlib-1.2.8 /usr/local/[root@localhost download]mv openssl-1.0.1c /usr/local/[root@localhost download]mv nginx-1.2.8 /usr/local/[root@localhost download]cd /usr/local/[root@localhost local]# cd pcre-8.36[root@localhost pcre-8.36]# ./configure&&make&&make install[root@localhost pcre-8.36] cd ../zlib-1.2.8[root@localhost zlib-1.2.8]# ./configure && make && make install[root@localhost zlib-1.2.8]# cd ../openssl-1.0.1c[root@localhost openssl-1.0.1c]# ./config && make && make install[root@localhost openssl-1.0.1c]# cd ../nginx-1.2.8[root@localhost nginx-1.2.8]# ./configure --prefix=/usr/local/nginx && make && make install

7、啟動nginx

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

8、測試

在確保可以連接到服務器的電腦上,瀏覽器輸入裝了nginx的機器的ip地址,會看到Welcome to nginx!的提示說明安裝配配置成功了。

CentOS6.5,安裝,nginx,負載均衡,配置

9、設置開機自動啟動(shell腳本處理)

[root@localhost logs]# vi /etc/init.d/nginx

添加以下shell腳本。

#!/bin/bash# nginx Startup script for the Nginx HTTP Server# it is v.0.0.2 version.# chkconfig: - 85 15# description: Nginx is a high-performance web and proxy server.#       It has a lot of features, but it's not for everyone.# processname: nginx# pidfile: /var/run/nginx.pid# config: /usr/local/nginx/conf/nginx.confnginxd=/usr/local/nginx/sbin/nginxnginx_config=/usr/local/nginx/conf/nginx.confnginx_pid=/var/run/nginx.pidRETVAL=0prog="nginx"# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ ${NETWORKING} = "no" ] && exit 0[ -x $nginxd ] || exit 0# Start nginx daemons functions.start() {if [ -e $nginx_pid ];then  echo "nginx already running...."  exit 1fi  echo -n $"Starting $prog: "  daemon $nginxd -c ${nginx_config}  RETVAL=$?  echo  [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx  return $RETVAL}# Stop nginx daemons functions.stop() {    echo -n $"Stopping $prog: "    killproc $nginxd    RETVAL=$?    echo    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid}# reload nginx service functions.reload() {  echo -n $"Reloading $prog: "  #kill -HUP `cat ${nginx_pid}`  killproc $nginxd -HUP  RETVAL=$?  echo}# See how we were called.case "$1" instart)    start    ;;stop)    stop    ;;reload)    reload    ;;restart)    stop    start    ;;status)    status $prog    RETVAL=$?    ;;*)    echo $"Usage: $prog {start|stop|restart|reload|status|help}"    exit 1esacexit $RETVAL

保存后,設置權限讓所有人可以操作。

[root@localhost ~]# chmod a+x /etc/init.d/nginx[root@localhost logs]# /etc/init.d/nginx statusnginx (pid 2417 2416) is running...[root@localhost logs]# /etc/init.d/nginx stopStopping nginx: [ OK ][root@localhost logs]# /etc/init.d/nginx startStarting nginx: [ OK ][root@localhost logs]# /etc/init.d/nginx statusnginx (pid 2454 2452) is running...[root@localhost ~]# vi /etc/rc.local

添加:

/etc/init.d/nginx start

重啟電腦就會生效。

注意:腳本里面關于啟動目錄的,如果你的安裝地址不一樣,要進行修改。

負載均衡配置:

現有兩部服務器:

192.168.137.197     (按照以上操作安裝有nginx,作為轉發機,虛擬機)
192.168.137.33     (無安裝nginx,運行有測試用的web工程,虛擬機)
10.10.33.59    (無安裝nginx,運行有測試用的web工程,本地電腦地址)
,運行有測試用的web工程
127.0.0.1 web_app

[root@a conf]# vi /usr/local/nginx/conf/nginx.conf

配置如下展示:

worker_processes 8;pid /usr/local/nginx/nginx.pid;worker_rlimit_nofile 102400;events{use epoll;worker_connections 102400;}http{ include    mime.types; default_type application/octet-stream; fastcgi_intercept_errors on; charset utf-8; server_names_hash_bucket_size 128; client_header_buffer_size 4k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush   on; keepalive_timeout 60; tcp_nodelay on; client_body_buffer_size 512k; proxy_connect_timeout  5; proxy_read_timeout    60; proxy_send_timeout    5; proxy_buffer_size    16k; proxy_buffers      4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 128k; gzip on; gzip_min_length 1k; gzip_buffers   4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types    text/plain application/x-javascript text/css application/xml; gzip_vary on;###2012-12-19 change nginx logslog_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '       '$status $body_bytes_sent "$http_referer" '       '"$http_user_agent" $request_time $remote_addr';upstream web_app {server 192.168.137.197:8080 weight=1 max_fails=2 fail_timeout=30s;server 192.168.137.33:8080 weight=1 max_fails=2 fail_timeout=30s;server 10.10.33.59:8080 weight=1 max_fails=2 fail_timeout=30s;}####chinaapp.sinaapp.comserver {  listen 80;  server_name chinaapp.sinaapp.com;  index index.jsp index.html index.htm;  #發布目錄/data/www  root /data/www;  location /  {  proxy_next_upstream http_502 http_504 error timeout invalid_header;  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_pass http://web_app;  expires   3d;  } }}

設置host:

[root@a conf]# vi /etc/hosts

添加hosts:

127.0.0.1 web_app

啟動nginx

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

啟動三部服務器的web應用:

在192.168.137.197執行指令:

[root@a PROGRAM]# curl "http://web_app/index.jsp"

測試結果如下:

CentOS6.5,安裝,nginx,負載均衡,配置

常見問題及解決辦法:

1、安裝PCRE時提示configure: error: You need a C++ compiler for C++ support.

原因是沒有安裝c++編譯器,采用下面的命令安裝:

[root@localhost pcre-8.37]# yum install -y gcc gcc-c++

2、啟動nginx失敗

[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf/usr/local/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

原因是64bit的系統,但是默認取了/usr/local/lib里面的包

檢查方法:

[root@localhost nginx]# cd /usr/local/[root@localhost local]# ls /lib64/ |grep pcrelibpcre.so.0libpcre.so.0.0.1[root@localhost local]# ls /lib/ |grep pcre

說明缺失的包在lib64

設置軟連接來解決:

[root@localhost local]# ln -s /lib64/libpcre.so.0.0.1 /lib64/libpcre.so.1[root@localhost local]# cd nginx[root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

3、啟動報錯:

報錯:nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

解決辦法:sudo fuser -k 80/tcp

端口被占用,關閉占用端口

希望本文所述對大家CentOS服務器配置有所幫助。


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郎溪县| 新宾| 鹤壁市| 云浮市| 永善县| 武乡县| 美姑县| 罗定市| 本溪| 克山县| 屯门区| 湘乡市| 油尖旺区| 永清县| 陵川县| 临西县| 邹城市| 临夏县| 库车县| 聂拉木县| 松阳县| 枣强县| 乐陵市| 连城县| 关岭| 黔西县| 秦皇岛市| 丹凤县| 抚远县| 防城港市| 蛟河市| 麻江县| 文水县| 得荣县| 札达县| 华池县| 神池县| 陈巴尔虎旗| 扎赉特旗| 蒲城县| 漯河市|