VUE 后台管理系统开发交流Q~群:942802115
软件版本
centos:6.8
nginx:1.14.2
安装
centos依赖安装
yum -y install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
- 下载nginx,下载存放目录 /usr/local/soft
mkdir -p /usr/local/soft/
cd /usr/local/soft/
wget http://nginx.org/download/nginx-1.14.2.tar.gz
解压下载包
tar -zxvf nginx-1.14.2.tar.gz
安装nginx
cd nginx-1.14.2
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make&&make install
======================= 安装完成 =================
个性化配置(按自己喜欢的方式,按业务需要的方式,不用非得按下文配置)
日志、配置目录
mkdir -p /data/nginx/logs
mkdir -p /data/nginx/conf
mkdir -p /data/nginx/html
拷贝配置文件
cp /usr/local/nginx/conf/mime.types /data/nginx/conf/
cp /usr/local/nginx/conf/nginx.conf /data/nginx/conf/
修改默认配置文件
vi /data/nginx/conf/nginx.conf
- 修改内容
error_log /data/nginx/logs/error.log;
pid /data/nginx/logs/nginx.pid;
worker_processes 4;
- 分文件配置代理
include /data/nginx/conf/custom.conf;
custom.conf样例
upstream my_mis {
server 172.20.238.171:8200;
}
server {
# 默认转发,加上default_server
#listen 80 default_server;
listen 80;
server_name mis.**.wang mis.**.com; # 多域名用逗号分隔开
# springboot 服务报错,找到不主机,需要下面几行
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass_request_headers on;
root /data/nginx/html;
index index.html;
# 如果以斜杠结尾的话,匹配到的路劲不会传递过去,只会传递路径后的子路劲
location /api/ {
proxy_pass http://my_mis;
}
location /oauth/ {
proxy_pass http://my_mis;
}
# vue history模式 前后分离配置样例
location / {
try_files $uri $uri/ /index.html;
#index index.html;
}
}
nginx.conf 参考
#user nobody;
worker_processes 4;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /data/nginx/logs/error.log;
pid /data/nginx/logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log off;#关闭日志
access_log /data/nginx/logs/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /data/nginx/conf/custom.conf;
include /data/nginx/conf/custom2.conf;
server {
listen 80 default_server;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /data/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
开机启动配置(不需要可不用关注)
配置文件
- 切换目录到/etc/init.d下创建文件nginx
vim /etc/init.d/nginx
- 文件内容可参考 nginx官方文档,内容如下:
#!/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
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
fi
}
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 $prog -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
- 修改一下两处文件内容:
- nginx="/usr/local/nginx/sbin/nginx" #修改成nginx执行程序的路径。
- NGINX_CONF_FILE=”/data/nginx/conf/nginx.conf” #修改成nginx.conf文件的路径。
- 保存并设置文件的执行权限
chmod a+x /etc/init.d/nginx
- 完成上述配置后,即可通过以下命令控制启动,停止等操作。
/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart
...
...
亦可将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx
完成之后,就可以使用service对nginx进行启动,重启等操作了。
service nginx start
service nginx stop
service nginx restart
- 设置NGINX开机自动启动
chkconfig nginx on
其他配置,后续完善。(如有问题,请留言,感谢指正)
VUE 后台管理系统开发交流Q~群:942802115