一、安装nginx: 以下操作均在root用户下:
1、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
2、cd /usr/local/src下
安装相关组件
wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
wget http://zlib.net/zlib-1.2.11.tar.gz
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
wget http://nginx.org/download/nginx-1.10.2.tar.gz
解析相关组件
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure && make && make install
tar zxvf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure && make && make install
tar zxvf openssl-fips-2.0.10.tar.gz
cd openssl-fips-2.0.10
./config && make && make install
tar zxvf nginx-1.10.2.tar.gz
cd nginx-1.10.2
./configure && make && make install
3、先找一下nginx安装到什么位置上了
----$ whereis nginx
---> nginx: /usr/local/nginx
4、进入nginx目录并启动
/usr/local/nginx/sbin/nginx 启动
/usr/local/nginx/sbin/nginx -s stop 停止
/usr/local/nginx/sbin/nginx -s reload 重启
ps -ef|grep nginx 查看是否启动
二、nginx配置文件 /usr/local/nginx/conf/nginx.conf
server {
listen 8000; #监听端口
server_name localhost; #域名
charset utf-8; #防止乱码
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location ^~ /check { #匹配以/check开头的url
rewrite ^/check/(.*) /$1 break; #加入这行正则匹配,会取消url中的 /check
root /home/aaa/nginx/html; #/home/aaa/nginx/html 这的是被代理的文件路径,url中会省略掉这部分
index index.html index.htm;
}
}
举例:
文件路径 /home/aaa/nginx/html/test/index.html
如果location 没有正则 " rewrite ^/check/(.*) /$1 break; "
则真正访问的url是 http://localhost:8000/check/test/index.html
但实际的文件路径是 /home/aaa/nginx/html/test/index.html
如果location 加上正则 " rewrite ^/check/(.*) /$1 break; "
则真正访问的url是 http://localhost:8000/test/index.html