Linux中安装Nginx
安装Nginx官网:http://nginx.org/en/download.html1.安装依赖yum -y install gcc pcre-devel openssl openssl-devel2.下载wget http://nginx.org/download/nginx-1.12.2.tar.gz3.解压到指定文件夹tar -zxvf nginx-...
·
安装Nginx
官网:http://nginx.org/en/download.html
1.安装依赖
yum -y install gcc pcre-devel openssl openssl-devel
2.下载
wget http://nginx.org/download/nginx-1.12.2.tar.gz
3.解压到指定文件夹
tar -zxvf nginx-1.12.2.tar.gz -C /usr/local/
4.创建nginx目录并进入加压的nginx目录
mkdir /usr/local/nginx
cd /usr/local/nginx-1.12.2
5.配置nginx(指定为刚才新建的目录)
./configure --prefix=/usr/local/nginx
6.编译并安装
make && make install
7.启动nginx(没有反应)
/usr/local/nginx/sbin/nginx
8.查看nginx进程
ps -ef | grep nginx
9.加入环境变量
vi /etc/profile
10.添加以下内容
PATH=$PATH:/usr/local/nginx/sbin
export PATH
11.保存后执行以下命令
source /etc/profile
12.开机自启脚本
-12.1.编辑文件
vi /etc/init.d/nginx
-12.2.复制以下内容(如有地址不同自定修改)
#!/bin/sh
# chkconfig: 2345 80 90
# description: Start and Stop nginx
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
kill -INT `cat $PIDFILE` || echo -n "nginx not running"
}
do_reload() {
kill -HUP `cat $PIDFILE` || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
-12.3.注册成服务、加入开机自启、给定执行权限
chkconfig --add nginx
chmod a+x /etc/init.d/nginx
chkconfig nginx on
-12.4.测试命令
service nginx start
service nginx stop
service nginx restart
13.配置nginx
-13.1.修改网站默认根目录路径
vi /usr/local/nginx/conf/nginx.conf
网站默认根目录放在/usr/local/nginx/html
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
-13.2.配置反向代理
server {
listen 80;
server_name nginx-01.cn; #nginx所在服务器的主机名
#反向代理的配置
location / { #拦截所有请求
root html;
proxy_pass http://192.168.0.21:8080; #这里是代理走向的目标服务器(IP:端口):tomcat
}
-13.3.动静分离
##动态资源
location ~ .*\.(jsp|do|action)$ {
proxy_pass http://tomcat-01.itcast.cn:8080;
}
##静态资源
location ~ .*\.(html|js|css|gif|jpg|jpeg|png)$ {
expires 3d;
}
-13.4.负载均衡
##在http这个节下面配置一个叫upstream的,后面的名字可以随意取,
##但是要和location下的proxy_pass http://后的保持一致
http {
#是在http里面的, 已有http, 不是在server里,在server外面
upstream tomcats {
server shizhan02:8080 weight=1; #weight表示权重
server shizhan03:8080 weight=1;
server shizhan04:8080 weight=1;
}
#写在server里
location ~ .*\.(jsp|do|action) {
proxy_pass http://tomcats; #tomcats是后面的tomcat服务器组的逻辑组号
}
}
更多推荐
已为社区贡献7条内容
所有评论(0)