Nginx源码编译安装与负载均衡配置实践
2012-07-23 17:29
711 查看
参考文档:
http://blog.s135.com/post/306/
http://wangyan.org/blog/install-nginx-from-source.html
安装与配置实践:
1. 安装pcre库,使Nginx支持正则表达式
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
# tar -xzvf pcre-8.30.tar.gz
注:不需要执行编译和安装过程(系统通常已经安装有较低版本的RPM包),因此仅作为编译Nginx时的引用。
2. 安装zlib库
wget http://sourceforge.net/projects/libpng/files/zlib/1.2.6/zlib-1.2.6.tar.gz/download
tar -xzvf zlib-1.2.6.tar.gz
注:不需要执行编译和安装过程(系统通常已经安装有较低版本的RPM包),因此仅作为编译Nginx时的引用。
3. 编译安装Nginx
# wget http://nginx.org/download/nginx-1.1.9.tar.gz
# tar -xzvf nginx-1.1.9.tar.gz
# mkdir -p /opt/nginx/tmp
# mkdir -p /opt/nginx/run
# mkdir -p /opt/nginx/lock
# useradd nginx
# cd nginx-1.1.9
# ./configure --prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--pid-path=/opt/nginx/run/nginx.pid \
--lock-path=/opt/nginx/lock/nginx.lock \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-pcre=../pcre-8.30 \
--with-zlib=../zlib-1.2.6 \
--with-debug \
--http-client-body-temp-path=/opt/nginx/tmp/client \
--http-proxy-temp-path=/opt/nginx/tmp/proxy \
--http-fastcgi-temp-path=/opt/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/opt/nginx/tmp/uwsgi \
--http-scgi-temp-path=/opt/nginx/tmp/scgi
参数详解:
--prefix #nginx安装目录,默认在/usr/local/nginx
--user=nginx #运行nginx的用户
--group=nginx #运行nginx的用户组
--pid-path #pid问件位置,默认在logs目录
--lock-path #lock问件位置,默认在logs目录
--with-http_ssl_module #开启HTTP SSL模块,以支持HTTPS请求。
--with-http_dav_module #开启WebDAV扩展动作模块,可为文件和目录指定权限
--with-http_flv_module #支持对FLV文件的拖动播放
--with-http_realip_module #支持显示真实来源IP地址
--with-http_gzip_static_module #预压缩文件传前检查,防止文件被重复压缩
--with-http_stub_status_module #取得一些nginx的运行状态
--with-mail #允许POP3/IMAP4/SMTP代理模块
--with-mail_ssl_module #允许POP3/IMAP/SMTP可以使用SSL/TLS
--with-pcre=../pcre-8.11 #指定未安装的pcre路径
--with-zlib=../zlib-1.2.5 #注意是未安装的zlib路径
--with-debug #允许调试日志
--http-client-body-temp-path #客户端请求临时文件路径
--http-proxy-temp-path #设置http proxy临时文件路径
--http-fastcgi-temp-path #设置http fastcgi临时文件路径
--http-uwsgi-temp-path #设置uwsgi 临时文件路径
--http-scgi-temp-path #设置scgi 临时文件路径
# make && make install
安装完成后会看到以下信息:
---
Configuration summary
+ using PCRE library: ../pcre-8.30
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using zlib library: ../zlib-1.2.6
nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/run/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "/opt/nginx/tmp/client"
nginx http proxy temporary files: "/opt/nginx/tmp/proxy"
nginx http fastcgi temporary files: "/opt/nginx/tmp/fastcgi"
nginx http uwsgi temporary files: "/opt/nginx/tmp/uwsgi"
nginx http scgi temporary files: "/opt/nginx/tmp/scgi"
---
4. 配置Nginx服务脚本
# mkdir -p /opt/nginx/init.d
# vim /opt/nginx/init.d/nginx
# chmod +x /opt/nginx/init.d/nginx
这样,就可以通过以下方式来管理Nginx服务:
# /opt/nginx/init.d/nginx start
# /opt/nginx/init.d/nginx stop
# /opt/nginx/init.d/nginx restart
# /opt/nginx/init.d/nginx reload
5. Nignx负载均衡配置
# cd /opt/nginx/conf/
# mv nginx.conf nginx.conf.bak
# vim /opt/nginx/conf/nginx.conf
启动Nginx服务
# /opt/nginx/init.d/nginx start
通过浏览器直接访问http://192.168.203.133,可以发现,在多次刷新之后,请求会随机分配到后端的Web服务器上。
http://blog.s135.com/post/306/
http://wangyan.org/blog/install-nginx-from-source.html
安装与配置实践:
1. 安装pcre库,使Nginx支持正则表达式
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
# tar -xzvf pcre-8.30.tar.gz
注:不需要执行编译和安装过程(系统通常已经安装有较低版本的RPM包),因此仅作为编译Nginx时的引用。
2. 安装zlib库
wget http://sourceforge.net/projects/libpng/files/zlib/1.2.6/zlib-1.2.6.tar.gz/download
tar -xzvf zlib-1.2.6.tar.gz
注:不需要执行编译和安装过程(系统通常已经安装有较低版本的RPM包),因此仅作为编译Nginx时的引用。
3. 编译安装Nginx
# wget http://nginx.org/download/nginx-1.1.9.tar.gz
# tar -xzvf nginx-1.1.9.tar.gz
# mkdir -p /opt/nginx/tmp
# mkdir -p /opt/nginx/run
# mkdir -p /opt/nginx/lock
# useradd nginx
# cd nginx-1.1.9
# ./configure --prefix=/opt/nginx \
--user=nginx \
--group=nginx \
--pid-path=/opt/nginx/run/nginx.pid \
--lock-path=/opt/nginx/lock/nginx.lock \
--with-http_ssl_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-mail \
--with-mail_ssl_module \
--with-pcre=../pcre-8.30 \
--with-zlib=../zlib-1.2.6 \
--with-debug \
--http-client-body-temp-path=/opt/nginx/tmp/client \
--http-proxy-temp-path=/opt/nginx/tmp/proxy \
--http-fastcgi-temp-path=/opt/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/opt/nginx/tmp/uwsgi \
--http-scgi-temp-path=/opt/nginx/tmp/scgi
参数详解:
--prefix #nginx安装目录,默认在/usr/local/nginx
--user=nginx #运行nginx的用户
--group=nginx #运行nginx的用户组
--pid-path #pid问件位置,默认在logs目录
--lock-path #lock问件位置,默认在logs目录
--with-http_ssl_module #开启HTTP SSL模块,以支持HTTPS请求。
--with-http_dav_module #开启WebDAV扩展动作模块,可为文件和目录指定权限
--with-http_flv_module #支持对FLV文件的拖动播放
--with-http_realip_module #支持显示真实来源IP地址
--with-http_gzip_static_module #预压缩文件传前检查,防止文件被重复压缩
--with-http_stub_status_module #取得一些nginx的运行状态
--with-mail #允许POP3/IMAP4/SMTP代理模块
--with-mail_ssl_module #允许POP3/IMAP/SMTP可以使用SSL/TLS
--with-pcre=../pcre-8.11 #指定未安装的pcre路径
--with-zlib=../zlib-1.2.5 #注意是未安装的zlib路径
--with-debug #允许调试日志
--http-client-body-temp-path #客户端请求临时文件路径
--http-proxy-temp-path #设置http proxy临时文件路径
--http-fastcgi-temp-path #设置http fastcgi临时文件路径
--http-uwsgi-temp-path #设置uwsgi 临时文件路径
--http-scgi-temp-path #设置scgi 临时文件路径
# make && make install
安装完成后会看到以下信息:
---
Configuration summary
+ using PCRE library: ../pcre-8.30
+ using system OpenSSL library
+ md5: using OpenSSL library
+ sha1: using OpenSSL library
+ using zlib library: ../zlib-1.2.6
nginx path prefix: "/opt/nginx"
nginx binary file: "/opt/nginx/sbin/nginx"
nginx configuration prefix: "/opt/nginx/conf"
nginx configuration file: "/opt/nginx/conf/nginx.conf"
nginx pid file: "/opt/nginx/run/nginx.pid"
nginx error log file: "/opt/nginx/logs/error.log"
nginx http access log file: "/opt/nginx/logs/access.log"
nginx http client request body temporary files: "/opt/nginx/tmp/client"
nginx http proxy temporary files: "/opt/nginx/tmp/proxy"
nginx http fastcgi temporary files: "/opt/nginx/tmp/fastcgi"
nginx http uwsgi temporary files: "/opt/nginx/tmp/uwsgi"
nginx http scgi temporary files: "/opt/nginx/tmp/scgi"
---
4. 配置Nginx服务脚本
# mkdir -p /opt/nginx/init.d
# vim /opt/nginx/init.d/nginx
001 | #!/bin/sh |
002 | # |
003 | # nginx - this script starts and stops the nginx daemin |
004 | # |
005 | # chkconfig: - 85 15 |
006 | # description:Nginx is an HTTP(S) server, HTTP(S) reverse \ |
007 | # proxy and IMAP/POP3 proxy server |
008 | # processname: nginx |
009 | # config:/usr/local/nginx/conf/nginx.conf |
010 | # pidfile: /usr/local/nginx/logs/nginx.pid |
011 |
012 | # Source function library. |
013 | . /etc/rc.d/init.d/functions |
014 |
015 | # Source networking configuration. |
016 | . /etc/sysconfig/network |
017 |
018 | # Check that networking is up. |
019 | [ "$NETWORKING" = "no" ] && exit 0 |
020 |
021 | nginx= "/opt/nginx/sbin/nginx" |
022 | prog=$( basename $nginx) |
023 |
024 | NGINX_CONF_FILE= "/opt/nginx/conf/nginx.conf" |
025 |
026 | lockfile=/opt/nginx/lock/nginx.lock |
027 |
028 | start() { |
029 | [-x $nginx ] || exit 5 |
030 | [-f $NGINX_CONF_FILE ] || exit 6 |
031 | echo -n $ "Starting $prog: " |
032 | daemon $nginx -c $NGINX_CONF_FILE |
033 | retval=$? |
034 | echo |
035 | [$retval - eq 0 ] && touch $lockfile |
036 | return $retval |
037 | } |
038 |
039 | stop() { |
040 | echo -n $ "Stopping $prog: " |
041 | killproc $prog -QUIT |
042 | retval=$? |
043 | echo |
044 | [$retval - eq 0 ] && rm -f $lockfile |
045 | return $retval |
046 | } |
047 |
048 | restart() { |
049 | configtest || return $? |
050 | stop |
051 | start |
052 | } |
053 |
054 | reload() { |
055 | configtest || return $? |
056 | echo -n $ "Reloading $prog: " |
057 | killproc $nginx -HUP |
058 | RETVAL=$? |
059 | echo |
060 | } |
061 |
062 | force_reload() { |
063 | restart |
064 | } |
065 |
066 | configtest() { |
067 | $nginx -t -c $NGINX_CONF_FILE |
068 | } |
069 |
070 | rh_status() { |
071 | status $prog |
072 | } |
073 |
074 | rh_status_q() { |
075 | rh_status >/dev/null 2>&1 |
076 | } |
077 |
078 | case "$1" in |
079 | start) |
080 | rh_status_q && exit 0 |
081 | $1 |
082 | ;; |
083 | stop) |
084 | rh_status_q || exit 0 |
085 | $1 |
086 | ;; |
087 | restart|configtest) |
088 | $1 |
089 | ;; |
090 | reload) |
091 | rh_status_q || exit 7 |
092 | $1 |
093 | ;; |
094 | force-reload) |
095 | force_reload |
096 | ;; |
097 | status) |
098 | rh_status |
099 | ;; |
100 | condrestart|try-restart) |
101 | rh_status_q || exit 0 |
102 | ;; |
103 | *) |
104 | echo $ "Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" |
105 | exit 2 |
106 | esac |
这样,就可以通过以下方式来管理Nginx服务:
# /opt/nginx/init.d/nginx start
# /opt/nginx/init.d/nginx stop
# /opt/nginx/init.d/nginx restart
# /opt/nginx/init.d/nginx reload
5. Nignx负载均衡配置
# cd /opt/nginx/conf/
# mv nginx.conf nginx.conf.bak
# vim /opt/nginx/conf/nginx.conf
01 | usernginx nginx; |
02 |
03 | worker_processes 10; |
04 |
05 | error_loglogs/error.log; |
06 |
07 | worker_rlimit_nofile 51200; |
08 |
09 | events |
10 | { |
11 | use epoll; |
12 | worker_connections 51200; |
13 | } |
14 |
15 | http |
16 | { |
17 | include mime.types; |
18 | default_typeapplication/octet-stream; |
19 |
20 | keepalive_timeout 120; |
21 |
22 | tcp_nodelay on; |
23 |
24 | upstream192.168.203.133{ |
25 | #ip_hash; |
26 | server 192.168.203.134:80; |
27 | server 192.168.203.135:80; |
28 | server 192.168.203.136:80; |
29 | server 192.168.203.137:80; |
30 | } |
31 |
32 | server |
33 | { |
34 | listen80; |
35 | server_name192.168.203.133; |
36 |
37 | location / { |
38 | proxy_passhttp://192.168.203.133; |
39 | proxy_set_header Host $host; |
40 | proxy_set_header X-Real-IP$remote_addr; |
41 | proxy_set_header X-Forwarded-For$proxy_add_x_forwarded_for; |
42 | } |
43 |
44 | log_format192_168_203_133 '$remote_addr - $remote_user [$time_local] $request ' |
45 | '"$status" $body_bytes_sent "$http_referer" ' |
46 | '"$http_user_agent" "$http_x_forwarded_for"' ; |
47 | access_log/opt/nginx/logs/cluster.log 192_168_203_133; |
48 | } |
49 | } |
# /opt/nginx/init.d/nginx start
通过浏览器直接访问http://192.168.203.133,可以发现,在多次刷新之后,请求会随机分配到后端的Web服务器上。
相关文章推荐
- Nginx源码编译安装与负载均衡配置实践
- 实战Nginx源码编译安装与配置
- 我个人的Nginx-1.12.2实践:安装,编译与测试(★firecat推荐,针对TCP四层负载均衡★)
- Nginx源码编译安装及配置文件解读
- 【服务器环境搭建-Centos】Nginx1.9.9 安装(编译源码)
- centos6.9源码安装nginx1.12.2并配置php7
- nginx编译安装与配置-配置篇
- centos安装nginx,配置负载均衡
- centos 下nginx源码编译安装
- 在Ubuntu Server上源码安装OpenERP(odoo) 8.0,并配置wsgi和nginx运行环境
- Linux(Ubuntu)下 vim/gvim/vimgdb 源码编译安装与配置(未完待续)
- x64+VS2013+opencv3.3.0自行编译源码安装及配置过程
- nginx 编译安装与配置
- 《深入理解Nginx》阅读与实践(一):Nginx安装配置与HelloWorld
- Linux下源码安装nginx服务器以及部分配置
- Linux 编译安装Nginx 配置 Python FastCGI Django (3) 配置nginx和django
- 源码编译安装,搭建LNMP环境(Linux+Nginx+MySQL+PHP)
- nginx 源码安装 及ssl配置
- ubuntu linux下源码编译安装nginx