Nginx
功能:
web服务器
web reverse proxy
smtp reverse proxy安装之前准备
1、依赖 gcc openssl-devel pcre-devel zlib-devel
安装:yum install gcc openssl-devel pcre-devel zlib-devel
2、创建用户和用户组。为了方便nginx运行而不影响linux安全
创建组:groupadd -r nginx
创建用户:useradd -r -g nginx -M nginx
-M 表示不创建用户的家目录。
一、安装Nginx:
1、到Nginx官网下载tar.gz包,运用SSH Secure Shell Client或者Xshell5,将windows环境下下载的包传入到linux系统的文件夹中
进入tar.gz包所在的文件夹,解压 tar-zxvf xxx.tar.gz ,进入 xxx.tar.gz,找到configure文件,进入(./configure)复制以下文本到配置文件中
--prefix=/usr \--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
--http-scgi-temp-path=/var/tmp/nginx/scgi \
--with-pcre
2、编译,安装
make && make install
二、配置Nginx为系统服务,以方便管理
1、在/etc/rc.d/init.d/目录中建立文本文件nginx
2、在文件中粘贴下面的内容:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
3、修改nginx文件的执行权限
chmod +x nginx
4、添加该文件到系统服务中去
chkconfig --add nginx
查看是否添加成功
chkconfig --list nginx
启动,停止,重新装载
service nginx start|stop
配置文件
worker_processes :worker进程的数量,一般根据CPU的个数
events {
worker_connections 1024; #每个worker的最大连接数。
}
htttp{
server{
#表示一个虚拟主机
}
}
location 映射
location [ = | ~ | ~* | ^~ ] uri { ... }
location URI {}:
对当前路径及子路径下的所有对象都生效;
location = URI {}:
精确匹配指定的路径,不包括子路径,因此,只对当前资源生效;
location ~ URI {}:
location ~* URI {}:
模式匹配URI,此处的URI可使用正则表达式,~区分字符大小写,~*不区分字符大小写;
location ^~ URI {}:
不使用正则表达式
优先级:= > ^~ > ~|~* > /|/dir/
-------------
location /bbs {
root /etc/html;
index index.html index.htm;
}
------------
访问控制
IP访问控制
location {
deny IP /IP段
deny 192.168.1.109;
allow 192.168.1.0/24;
}
用户认证访问
模块ngx_http_auth_basic_module 允许使用“HTTP基本认证”协议验证用户名和密码来限制对资源的访问。
location / {
auth_basic "closed site";
auth_basic_user_file /var/users;
}
Apache发行包中的htpasswd命令来创建user_file 文件
htpasswd -c -m /var/users username
autoindex 在浏览器中列出当前目录的文件索引
autoindex on;
启用访问状态模块
location /status{
stub_status on;
}
虚拟主机
一个server{} 就是一个虚拟主机
基于域名
反向代理:
proxy_pass
1、自动监测当机
2、自动心跳
location [op] URI {
proxy_pass http://192.168.100.11/;
}
比如:
location /forum/ {
proxy_pass http://172.16.100.11:8080/bbs/;
}
http://www.bjsxt.com/forum/
--> http://172.16.100.11:8080/bbs/;
location ~* ^/forum {
proxy_pass http://172.16.100.11:8080;
}
http://www.bjsxt.com/forum/ -->
http://172.16.100.11:8080/forum/
改变反向代理时,apache记录的日志
httpd.conf
LogFormat %{X-Real_IP}i