下载nginx # cd
# wget http://nginx.org/download/nginx-1.8.0.tar.gz 解压nginx
# tar zxvf nginx-1.8.0.tar.gz 配置编译参数 安装依赖包 centOS:
# yum install glib2-devel openssl-devel pcre-devel bzip2-devel gzip-devel Ubuntu: # apt-get update
# apt-get install libpcre3 libpcre3-dev libssl-dev openssl
# cd nginx-1.8.0 ./configure \\
--prefix=/opt/server/nginx1.8.0 \\
--error-log-path=/opt/var/logs/nginx/error.log \\ --http-log-path=/opt/var/logs/nginx/access.log \\ --with-http_realip_module \\ --with-http_sub_module \\ --with-http_gzip_static_module \\ --with-http_stub_status_module \\ --with-pcre 编译nginx make
安装nginx make install
编写nginx启动脚本,并加入系统服务 CentOS: vim /etc/init.d/nginx 写入如下内容: #!/bin/bash # chkconfig: - 30 21 # description: http service. # Source Function Library . /etc/init.d/functions # Nginx Settings
NGINX_SBIN=\"/opt/server/nginx1.8.0/sbin/nginx\" NGINX_CONF=\"/opt/server/nginx1.8.0/conf/nginx.conf\" NGINX_PID=\"/tmp/nginx.pid\" RETVAL=0 prog=\"Nginx\" start() {
echo -n $\"Starting $prog: \" mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF RETVAL=$? echo
return $RETVAL } stop() {
echo -n $\"Stopping $prog: \"
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp RETVAL=$? echo
return $RETVAL } reload(){
echo -n $\"Reloading $prog: \"
killproc -p $NGINX_PID $NGINX_SBIN -HUP RETVAL=$? echo
return $RETVAL } restart(){ stop start }
configtest(){
$NGINX_SBIN -c $NGINX_CONF -t return 0 }
case \"$1\" in start) start ;; stop) stop ;; reload) reload ;;
restart) restart ;; configtest) configtest ;; *)
echo $\"Usage: $0 {start|stop|reload|restart|configtest}\" RETVAL=1 esac
exit $RETVAL 保存后,更改权限: chmod 755 /etc/init.d/nginx chkconfig --add nginx 如果想开机启动,请执行: chkconfig nginx on Ubuntu:
# cat /etc/init.d/nginx #!/bin/sh
### BEGIN INIT INFO # Provides: nginx # Required-Start: # Required-Stop:
# Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: nginx # Description: nginx server ### END INIT INFO . /lib/lsb/init-functions
PROGRAM=/opt/server/nginx1.8.0/sbin/nginx test -x $PROGRAM || exit 0 case \"$1\" in start)
log_begin_msg \"Starting Nginx server\" /opt/server/nginx1.8.0/sbin/nginx log_end_msg 0 ;; stop)
PID=`cat /tmp/nginx.pid`
log_begin_msg \"Stopping Nginx server\" if [ ! -z \"$PID\" ]; then kill -15 $PID fi
log_end_msg 0 ;; restart) $0 stop $0 start ;; *)
log_success_msg \"Usage: service nginx {start|stop|restart}\" exit 1 esac exit 0
Ubuntu开机启动:
# chmod 755 /etc/init.d/nginx # update-rc.d nginx defaults 更改nginx配置
首先把原来的配置文件清空: >/opt/server/nginx1.8.0/conf/nginx.conf
“>” 这个符号之前阿铭介绍过,为重定向的意思,单独用它,可以把一个文本文档快速清空。
vim /opt/server/nginx1.8.0/conf/nginx.conf 写入如下内容: user nobody;
pid /tmp/nginx.pid;
worker_processes 4; #根据CPU核数 worker_rlimit_nofile 100000; events {
worker_connections 2048; multi_accept on; use epoll; } http {
include mime.types; server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; #access_log off;
#error_log /var/log/nginx/error.log crit; #编译时已设置 keepalive_timeout 10; client_header_timeout 10; client_body_timeout 10; reset_timedout_connection on; send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; default_type text/html; charset UTF-8; gzip on;
gzip_disable \"msie6\"; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s;
open_file_cache_min_uses 2; open_file_cache_errors on;
include /opt/service/nginx1.8.0/conf.d/*.conf; include /opt/service/nginx1.8.0/sites-enabled/*; server {
listen 9090;
server_name localhost;
index index.html index.htm index.php; root /opt/server/nginx1.8.0/html; } }
保存配置后,先检验一下配置文件是否有错误存在: /opt/server/nginx1.8.0/sbin/nginx -t
如果显示内容如下,则配置正确,否则需要根据错误提示修改配置文件: nginx: the configuration file /opt/server/nginx1.8.0/conf/nginx.conf syntax is ok nginx: configuration file /opt/server/nginx1.8.0/conf/nginx.conf test is ssuccessful 启动nginx: # service nginx restart
如果不能启动,请查看 “/opt/server/nginx1.8.0/logs/error.log” 文件,检查nginx是否启动: # ps aux |grep nginx 看是否有进程。 配置Nginx反向代理
修改nginx反向代理配置文件,内容如下: #user nobody; pid /tmp/nginx.pid; worker_processes 4;
worker_rlimit_nofile 100000; events {
worker_connections 65536;
multi_accept on; use epoll; } http {
include mime.types; server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; #access_log off;
#error_log /var/log/nginx/error.log crit; keepalive_timeout 10; client_header_timeout 10; client_body_timeout 10; reset_timedout_connection on; send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; default_type text/html; charset UTF-8; gzip on;
gzip_disable \"msie6\"; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;
include /opt/service/nginx1.8.0/conf.d/*.conf; include /opt/service/nginx1.8.0/sites-enabled/*; upstream web_portal{
ip_hash;
server xxx.xxx.xxx.xxx:7101 max_fails=3 fail_timeout=60s;
}
upstream sfserver { hash $request_uri;
server xxx.xxx.xxx.xxxx:80 max_fails=3 fail_timeout=60s;
} server { listen 9090;
server_name xxx.xxx.xxx.xxx; index index.html index.htm index.jsp; location / {
proxy_pass http://web_portal/; proxy_set_header Host $host; client_max_body_size 1000M; } location /u/ {
proxy_pass http://sfserver/; proxy_set_header Host $host; client_max_body_size 1000M; }
location /nginx_status { stub_status on;
access_log off;
allow 127.0.0.1; #localhost
allow xxx.xxx.xxx.xxx; #zabbix server deny all; } } }
配置Nginx静态文件服务器
修改静态文件服务器nginx配置文件,内容如下: user root; pid /tmp/nginx.pid; worker_processes 4;
worker_rlimit_nofile 100000; events {
worker_connections 2048; multi_accept on; use epoll; } http {
include mime.types; server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; #access_log off;
#error_log /var/log/nginx/error.log crit; keepalive_timeout 10; client_header_timeout 10; client_body_timeout 10; reset_timedout_connection on; send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m; limit_conn addr 100; default_type text/html; charset UTF-8; gzip on;
gzip_disable \"msie6\"; gzip_proxied any; gzip_min_length 1000; gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml
application/xml application/xml+rss text/javascript; open_file_cache max=100000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on;
include /opt/service/nginx1.8.0/conf.d/*.conf; include /opt/service/nginx1.8.0/sites-enabled/*; ### static file server server {
listen 80;
server_name localhost; root /opt/data/static; location / { root /opt/data/static; } }
### zabbix server server { listen 9090;
server_name localhost;
index index.html index.htm index.php; root /opt/server/nginx1.8.0/html; location /nginx_status { stub_status on; access_log off;
allow 127.0.0.1; #localhosttest allow 120.16.1.116; #zabbix server deny all; } } } 测试
在静态文件服务器 /opt/data/static 下新建 test.html 文件,文件内容随意:
liuyang 111111
打开任意一个反向代理的IP的/u/子目录,如: http:/xxxx.xxx.xxx.xxx/u/test.html 如能正常显示测试文件内容,则OK。
因篇幅问题不能全部显示,请点此查看更多更全内容