最近項(xiàng)目需求速缨,需要自己搭建nginx锌妻,配置VUE項(xiàng)目。由于測(cè)試環(huán)境旬牲,一個(gè)服務(wù)器需要同時(shí)搭建多個(gè)VUE項(xiàng)目仿粹。
環(huán)境:Linux
MAC進(jìn)入linux系統(tǒng)
通過(guò)終端登錄進(jìn)入服務(wù)器
~ ssh 登錄名@需要登錄的服務(wù)器IP地址
安裝nginx
Linux 版本的編譯和安裝:準(zhǔn)備工作
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel
- Nginx 的官方下載地址為:http://nginx.org/en/download.html
- 下載 Nginx服務(wù)器全部歷史版本的鏈接:http://nginx.org/download/
官網(wǎng)上提供了Nginx 服務(wù)器三種版本的下載,分別是開(kāi)發(fā)版(Mainline version)原茅、穩(wěn)定版本(Stable version)和過(guò)期版本(Legacy versions)牍陌。
頁(yè)面上下載部分各鏈接的具體含義:
- “CHANGES-x.xx”鏈接,記錄的是對(duì)應(yīng)版本的功能變更日志员咽。包括新增功能毒涧、功能的優(yōu)化和功能缺陷的修復(fù)等。
- 緊接著“CHANGES-x.xx”鏈接后面的“nginx-x.x.x”鏈接,是 Nginx服務(wù)器的 Linux版本下載地址契讲。
- “pgp”鏈接仿吞,記錄的是提供下載的版本使用PGP加密自由軟件GnuPG計(jì)算后的簽名。PGP可以理解為Pretty Good Privacy捡偏。這些數(shù)據(jù)可以用于下載文件的驗(yàn)證唤冈。
- “nginx/Windows-x.x.x”鏈接,是 Nginx 服務(wù)器的Windows版本下載地址银伟。
安裝
選擇要安裝的nginx的文件目錄:
安裝在:etc目錄
下載nginx穩(wěn)定版本包:
進(jìn)入目錄:cd /usr/share/
下載包:wget http://nginx.org/download/nginx-1.14.2.tar.gz
解壓包:tar xf nginx-1.14.2.tar.gz
移動(dòng)gz進(jìn)入nginx-1.14.2:mv nginx-1.14.2 ./nginx-1.14.2/ 這一步只是為了以后可以在用你虹,可以將包直接刪除掉
打算把nginx安裝到路徑usr/local
進(jìn)入文件夾:cd /usr/local/
創(chuàng)建Nginx的安裝路徑:mkdir nginx
再次進(jìn)入解壓安裝包的文件夾,使用以下命令配置并生成Makefile 文件
./configure --prefix=/usr/local/nginx/
編譯和安裝:make install
部分文件介紹:
- src 目錄存放了 Nginx軟件的所有源代碼彤避。
- man 目錄中存放了Nginx 軟件的幫助文檔傅物,Nginx 安裝完成后,使用 man 命令可以查看琉预。
- html 目錄中存放了l 兩個(gè)后綴名為.html的靜態(tài)網(wǎng)頁(yè)文件董饰。這兩個(gè)文件與 Nginx服務(wù)器的運(yùn)行相關(guān)。
- conf 目錄存放的是Nginx 服務(wù)器的配置文件圆米,包含 Nginx服務(wù)器的基本配置文件和對(duì)部分特性的配置文件卒暂。
- auto 目錄中存放了大量腳本文件,和configure腳本程序有關(guān)娄帖。
- configure文件是 Nginx軟件的自動(dòng)腳本程序也祠。運(yùn)行 configure自動(dòng)腳本一般會(huì)完成兩項(xiàng)工作:一是檢查環(huán)境,根據(jù)環(huán)境檢查結(jié)果生成 C 代碼近速;二是生成編譯代碼需要的Makefile 文件诈嘿。
啟動(dòng)
進(jìn)入安裝的文件夾:cd /usr/local/nginx/
幫助: ./sbin/nginx -h
開(kāi)啟: ./sbin/nginx
或許端口被占用,可用命令查: netstat -lnp|grep 80 或者 netstat -ntlp
關(guān)閉端口: kill PID
幫助說(shuō)明:
nginx version: nginx/1.14.2
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx-1.14.2//)
-c filename : set configuration file (default: conf/nginx.conf)
-g directives : set global directives out of configuration file
配置為系統(tǒng)服務(wù)
一般情況数焊,安裝好 Nginx 后永淌,使用它的命令是 它的路徑+對(duì)應(yīng)的命令 ,但路徑很長(zhǎng)佩耳,每次使用都很麻煩遂蛀,現(xiàn)在添加一個(gè) ** service nginx xxx ** 的方式,簡(jiǎn)單快捷干厚,在很多教程中也是如此使用李滴。
需要在 /etc/init.d/ 下面創(chuàng)建文件 nginx,把下面的代碼考到nginx文件里面
cd /etc/init.d/
touch nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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
注意事項(xiàng)
nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
路徑一定對(duì)應(yīng)上
給權(quán)限: chmod 755 /etc/init.d/nginx
chkconfig 命令: chkconfig --add nginx
service nginx restart
配置項(xiàng)說(shuō)明
- Nginx 配置文件 server 中指定兩個(gè) location 執(zhí)行蛮瞄,分別為root 和 alias 指令:
location /test/ {
alias /www/test/;
}
按照上述配置所坯,則訪問(wèn) /test/ 目錄里面的文件時(shí),nginx 會(huì)去 /www/test/ 目錄找文件
location /test/ {
root /www/test;
}
按照這種配置挂捅,則訪問(wèn) /test/ 目錄下的文件時(shí)芹助,nginx 會(huì)去 /www/test/test/ 目錄下找文件
alias 是一個(gè)目錄別名的定義,root 則是最上層目錄的定義。
另一個(gè)區(qū)別是 alias 后面必須要用 “/” 結(jié)束状土,否則會(huì)找不到文件无蜂,而 root 則對(duì) ”/” 可有可無(wú)。
nginx.conf 內(nèi)容
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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;
# }
#}
}
上面的內(nèi)容對(duì)應(yīng)結(jié)構(gòu)是下圖:
啟動(dòng)前端服務(wù)
把前端打包好的項(xiàng)目放到目錄 /usr/share/nginx-1.14.2/html/
配置頁(yè)面路徑
前后端分離蒙谓,需要配置跨域
VUE單頁(yè)項(xiàng)目斥季,通過(guò)hash或者h(yuǎn)istroy這樣方式進(jìn)行跳轉(zhuǎn)頁(yè)面,所以需要配置重定向跳轉(zhuǎn)index.html
遇到問(wèn)題
啟動(dòng)的時(shí)候找不到 nginx.pid /etc/nginx//logs/nginx.pid
解決方案
./sbin/nginx -c /etc/nginx/conf/nginx.conf
開(kāi)啟https不能啟動(dòng)累驮,找不到ssl模塊
需要安裝模塊
看下編譯安裝nginx的時(shí)候酣倾,都編譯安裝的哪些模塊。
1.輸入命令
/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
configure arguments:
2.下載相應(yīng)的nginx源碼包放在/usr/local/src下解壓,進(jìn)入解壓后的目錄執(zhí)行
./configure --with-http_ssl_module //重新添加這個(gè)ssl模塊
3.執(zhí)行以上一條命令出現(xiàn)這個(gè)錯(cuò)誤(./configure:錯(cuò)誤:SSL模塊需要OpenSSL庫(kù)谤专。)躁锡,原因是因?yàn)槿鄙倭薕penSSL,那我們?cè)賮?lái)安裝一個(gè)即可執(zhí)行:
yum -y install openssl openssl-devel
4.執(zhí)行./configure,再執(zhí)行
./configure --with-http_ssl_module
5.執(zhí)行make命令毒租,但是不要執(zhí)行make install稚铣,因?yàn)閙ake是用來(lái)編譯的箱叁,而make install是安裝墅垮,不然你整個(gè)nginx會(huì)重新覆蓋的。
6.在我們執(zhí)行完做命令后耕漱,我們可以查看到在nginx解壓目錄下算色,objs文件夾中多了一個(gè)nginx的文件,這個(gè)就是新版本的程序了螟够。首先我們把之前的nginx先備份一下灾梦,然后把新的程序復(fù)制過(guò)去覆蓋之前的即可。
cp /etc/nginx /etc/nginx/sbin/nginx.bak
cp /usr/share/nginx/nginx-1.14.2/objs/nginx /etc/nginx/sbin/nginx
最后可以啟動(dòng)了妓笙。