Nginx安裝
參考:
- 學習視頻
- https://www.cnblogs.com/EasonJim/p/7806879.html
- http://www.cnblogs.com/piscesLoveCc/p/5794926.html
- http://www.linuxidc.com/Linux/2016-08/134080.htm
- https://segmentfault.com/a/1190000002797601
- https://www.jianshu.com/p/99aac00aa3b7
各種環境的nginx的關於安裝的匯總...有點亂
Nginx版本
- Mainline version-開發版
- Stable version-穩定版
- Legacy version-歷史穩定版本
可以到 http://nginx.org/en/download.html 下載需要的版本
win
Nginx+Tomcat的集群配置:
1、2步驟解壓java/Nginx中的window集群就可以使用了;
3要使用單獨的Nginx壓縮包;
1)在一台電腦上安裝兩個tomcat
需要在一台電腦模擬:在E盤解壓兩個tomcat,分別命名為tomcat1,tomcat2.
2)修改tomcat的配置文件,將端口進行修改:
分別完成如下配置:(需要將tomcat帶有端口號的地方改成不同的端口即可.)分別打開兩個tomcat的conf下的server.xml
- tomcat1/conf/server.xml
<Server port="8005" shutdown="SHUTDOWN">
<!--端口號8080-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
- tomcat2/conf/server.xml:所有端口+10
<Server port="8015" shutdown="SHUTDOWN">
<!--端口號8090-->
<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8453" />
<Connector port="8019" protocol="AJP/1.3" redirectPort="8453" />
<Engine name="Catalina" defaultHost="localhost">
3)將項目分別發布到兩個tomcat中:
- 打開eclipse;
- 選中項目,右鍵export--war file--選擇位置;
- 復制項目到Tomcat的webapps下;
測試:
a.啟動Tomcat:bin/startup.bat
b.瀏覽器:localhost:8080/項目名;
4)安裝Nginx:
1.解壓nginx-1.8.0;
2.雙擊nginx.exe
啟動完以后訪問http://localhost
5)配置Nginx:
修改nginx-1.8.0/conf/nginx.conf文件:
1.需要在http節點上添加一個:
upstream servlet_yujia{ //upstream后跟自己起的名稱
server 127.0.0.1:8080; //代理的服務器的接口
server 127.0.0.1:8090;
}
2.修改location /下的反向代理 :
proxy_pass http://servlet_yujia //http:后跟名稱
6)Tomcat集群的session共享:
1.一種解決辦法:一個用戶進來以后只在tomcat1上進行操作,另一個用戶進行只在tomcat2上進行操作.
2.session的共享
一種使用tomcat廣播機制完成session的共享(不推薦的方式)
一種使用redis服務器的方式完成session的共享(推薦的方式)
- 解決方式1:只能在window下好使(下方有7)
web服務器解決(廣播機制)
注意:tomcat下性能低,但是在weblogic上很好
修改兩個地方:
1.修改每個tomcat的server.xml 支持共享
將引擎標簽下的
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
注釋去掉
2.修改每個項目的配置文件 web.xml(/tomcat/webapps/項目/WEB-INF/web.xml)中添加一個節點
<distributable/>
3.重啟Tomcat
現在再訪問項目,在不同Tomcat中的項目的id不變;
- 解決方式2:
可以將session的id放入redis中 - 解決方式3:在linux使用多
保證一個ip地址永遠的訪問一台web服務器,就不存在session共享問題了
在nginx的配置文件中upstream
中添加ip_hash
;
linux
環境確認:可能會有影響
1、確認系統網絡
2、確認yum可用
3、確認關閉iptables規則
4、確認停用selinux
ubuntu安裝nginx
方法1:基於APT源安裝
sudo apt-get install nginx
- /usr/sbin/nginx:主程序
- /etc/nginx:存放配置文件
- /usr/share/nginx:存放靜態文件
- /var/log/nginx:存放日志
Linux系統的配置文件一般放在
/etc
,日志一般放在/var/log
,運行的程序一般放在/usr/sbin
或者/usr/bin
。
如果要更清楚Nginx的配置項放在什么地方,可以打開/etc/nginx/nginx.conf
通過這種方式安裝的,會自動創建服務,會自動在/etc/init.d/nginx
新建服務腳本
- 查看加載的是哪個配置文件
sudo nginx -t或者ps -ef | grep nginx
- 啟動/停用服務
sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
方法2:命令行+壓縮文件安裝
- 官方下載頁面:http://nginx.org/en/download.html
- configure配置文件詳解:http://nginx.org/en/docs/configure.html
- 檢查Linux內核
# inux的內核版本要在2.6以上
uname -a
- 安裝依賴
#1.安裝gcc g++的依賴庫
sudo apt-get install build-essential
sudo apt-get install libtool
#2.安裝pcre依賴庫([http://www.pcre.org/](http://www.pcre.org/))
sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
#3.安裝zlib依賴庫([http://www.zlib.net](http://www.zlib.net/))
sudo apt-get install zlib1g-dev
#4.安裝SSL依賴庫(16.04默認已經安裝了)
sudo apt-get install openssl
- 安裝Nginx
Nginx的官網下載地址是:http://nginx.org/en/download.html ,點擊進入,
#下載最新版本:復制上面的Linux的下載鏈接,wget下載(我當時是1.13.6版本) 進入到想要放置文件的目錄
wget http://nginx.org/download/nginx-1.13.6.tar.gz
#解壓:
tar -zxvf nginx-1.13.6.tar.gz
#進入解壓目錄:
cd nginx-1.13.6
#配置:
./configure --prefix=/usr/local/nginx
# --with-http_stub_status_module 加入http_stub_status(用來做連接數檢測)模塊
# --with-http_ssl_module http_ssl(https協議)模塊
# --with-debug 打開debug開關
#編譯:
make
#安裝:安裝在/usr/local/nginx
sudo make install
#啟動:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,可以通過-h查看幫助命令。
#查看進程:
ps -ef | grep nginx
配置
- 配置軟鏈接:就可以不用路徑直接輸入nginx啟動
sudo ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
- 配置開機啟動服務
在/etc/init.d/
下創建nginx文件,sudo vim /etc/init.d/nginx
,內容如下:
#!/bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/nginx/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
. /etc/default/nginx
fi
STOP_SCHEDULE="${STOP_SCHEDULE:-QUIT/5/TERM/5/KILL/5}"
test -x $DAEMON || exit 0
. /lib/init/vars.sh
. /lib/lsb/init-functions
# Try to extract nginx pidfile
PID=$(cat /usr/local/nginx/conf/nginx.conf | grep -Ev '^\s*#' | awk 'BEGIN { RS="[;{}]" } { if ($1 == "pid") print $2 }' | head -n1)
if [ -z "$PID" ]; then
PID=/run/nginx.pid
fi
if [ -n "$ULIMIT" ]; then
# Set ulimit if it is set in /etc/default/nginx
ulimit $ULIMIT
fi
start_nginx() {
# Start the daemon/service
#
# Returns:
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON --test > /dev/null \
|| return 1
start-stop-daemon --start --quiet --pidfile $PID --exec $DAEMON -- \
$DAEMON_OPTS 2>/dev/null \
|| return 2
}
test_config() {
# Test the nginx configuration
$DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
}
stop_nginx() {
# Stops the daemon/service
#
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet --retry=$STOP_SCHEDULE --pidfile $PID --name $NAME
RETVAL="$?"
sleep 1
return "$RETVAL"
}
reload_nginx() {
# Function that sends a SIGHUP to the daemon/service
start-stop-daemon --stop --signal HUP --quiet --pidfile $PID --name $NAME
return 0
}
rotate_logs() {
# Rotate log files
start-stop-daemon --stop --signal USR1 --quiet --pidfile $PID --name $NAME
return 0
}
upgrade_nginx() {
# Online upgrade nginx executable
# http://nginx.org/en/docs/control.html
#
# Return
# 0 if nginx has been successfully upgraded
# 1 if nginx is not running
# 2 if the pid files were not created on time
# 3 if the old master could not be killed
if start-stop-daemon --stop --signal USR2 --quiet --pidfile $PID --name $NAME; then
# Wait for both old and new master to write their pid file
while [ ! -s "${PID}.oldbin" ] || [ ! -s "${PID}" ]; do
cnt=`expr $cnt + 1`
if [ $cnt -gt 10 ]; then
return 2
fi
sleep 1
done
# Everything is ready, gracefully stop the old master
if start-stop-daemon --stop --signal QUIT --quiet --pidfile "${PID}.oldbin" --name $NAME; then
return 0
else
return 3
fi
else
return 1
fi
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
start_nginx
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
stop_nginx
case "$?" in
0|1) log_end_msg 0 ;;
2) log_end_msg 1 ;;
esac
;;
restart)
log_daemon_msg "Restarting $DESC" "$NAME"
# Check configuration before stopping nginx
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi
stop_nginx
case "$?" in
0|1)
start_nginx
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
reload|force-reload)
log_daemon_msg "Reloading $DESC configuration" "$NAME"
# Check configuration before stopping nginx
#
# This is not entirely correct since the on-disk nginx binary
# may differ from the in-memory one, but that's not common.
# We prefer to check the configuration and return an error
# to the administrator.
if ! test_config; then
log_end_msg 1 # Configuration error
exit $?
fi
reload_nginx
log_end_msg $?
;;
configtest|testconfig)
log_daemon_msg "Testing $DESC configuration"
test_config
log_end_msg $?
;;
status)
status_of_proc -p $PID "$DAEMON" "$NAME" && exit 0 || exit $?
;;
upgrade)
log_daemon_msg "Upgrading binary" "$NAME"
upgrade_nginx
log_end_msg $?
;;
rotate)
log_daemon_msg "Re-opening $DESC log files" "$NAME"
rotate_logs
log_end_msg $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}" >&2
exit 3
;;
esac
- 設置服務腳本有執行權限
sudo chmod +x /etc/init.d/nginx
- 注冊服務
cd /etc/init.d/
sudo update-rc.d nginx defaults
- 啟動/停用服務
sudo service nginx {start|stop|restart|reload|force-reload|status|configtest|rotate|upgrade}
修改端口
nginx默認使用的端口是80,如果端口已經被占據,那么需要修改默認端口!默認的配置在安裝文件夾下的conf文件夾下的ngixn.conf文件中,目錄為 /usr/local/nginx/conf
vim /usr/local/nginx/conf/ngixn.conf
# 將 server中的listen 中對應的就是端口號了
比如,改為8080
常用命令
- 啟動方式
#默認方式啟動:
/usr/local/nginx/sbin/nginx
#指定配置文件啟動
/usr/local/nginx/sbing/nginx -c /tmp/nginx.conf
##-c 指定配置文件的路徑,不加的話,nginx會自動加載默認路徑的配置文件,可以通過-h查看幫助命令。
#指定nginx程序目錄啟動
/usr/local/nginx /sbin/nginx -p /usr/local/nginx/
- 停止方式
#快速停止
/usr/local/nginx/sbin/nginx -s stop
#優雅停止
/usr/local/nginx/sbin/nginx -s quit
- 熱裝載配置文件 ,不用停止可以刷新配置
/usr/local/nginx/sbin/nginx -s reload
- 重新打開日志文件
/usr/local/nginx/sbin/nginx -s reopen
- 檢測當前使用的是哪個配置文件,配置是否正確
/usr/local/nginx/sbin/nginx -t #可以在配置文件加點亂碼測試一下
日志切割備份
默認日志是在/usr/local/nginx/logs/
文件夾中,默認寫在access.log
中
mv access.log access.log.bak
/usr/local/nginx/sbin/nginx -s reopen # 重新打開日志文件,寫入新文件中(若是不運行一下,寫入的還是舊文件,就是上面的.bat的文件)
centos安裝
預處理:
關閉iptables:
iptables -L #查看是否開啟
iptables -F #關閉
iptables -t nat -L #查看net中是否有規則
iptables -t nat -F #關閉nat規則
停用selinux:
getenforce #查看是否關閉 disabled 代表關閉了
setenforce 0 #關閉
安裝
在http://nginx.org/en/download.html 的Pre-Built Packages
的stable and mainline
連接進入就可以看到官方提供的yum的安裝方法.此處使用的是RHEL/CentOS
的安裝方法
#yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake
# yum -y install wget httpd-tools vim
# 首先運行
sudo yum install yum-utils
# 創建一個nginx的配置文件
vim /etc/yum.repos.d/nginx.repo
# 配置
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
# 獲取所有的版本
sudo yum list | grep nginx
# 下載最新穩定版
sudo yum install nginx
# 查看安裝版本
nginx -v
# 查看編譯參數
nginx -V
Linux上搭建Nginx+Tomcat集群:
在Linux上安裝多個Tomcat:
- 解壓tomcat
分別解壓tomcat到/usr/local/tomcat1 和 tomcat2 - 修改tomcat2中server.xml:
將修改后的端口添加到防火牆中. - Linux上安裝Nginx:
1.先將 nginx上傳到linux上
2.解壓nginx(放在usr/local中 或放在自己建立的software中)
tar -xvf 文件;
3.進入目錄 cd xxx
4.安裝依賴包
yum install gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
5.執行編譯
先進入 nginx的目錄
執行 ./configure
可以通過在upstream下設置一個ip_hash
解決session共享問題
ip_hash
指令能夠將某個客戶端IP的請求通過哈希算法定位到同一台后端服務器上
啟動
在nginx目錄下有一個sbin
目錄,sbin目錄下有一個nginx可執行程序。
./nginx
關閉nginx
關閉命令:相當於找到nginx進程kill。
./nginx -s stop
退出命令:等程序執行完畢后關閉,建議使用此命令。
./nginx -s quit
動態加載配置文件
可以不關閉nginx的情況下更新配置文件。
./nginx -s reload
docker安裝nginx
version: '3.6'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest #鏡像
container_name: nginx # 容器名
ports:
- 80:80
- 進入文件夾
/opt/docker_nginx/docker-compose.yml
,將上面的內容復制進去 - 運行
docker-compose up -d
- 打開瀏覽器,訪問這個服務器的80端口,就能進入docker的首頁
nginx配置
默認在容器的
/etc/nginx
中
- 進入nginx容器:
docker exec -it nginx bash
- 進入:
cd /etc/nginx
- 打開配置文件:
cat nginx.conf
/etc/nginx/nginx.conf
文件
user nginx;
worker_processes 1; # worker_processes的值越大,Nginx的並發能力越強
error_log /var/log/nginx/error.log warn; # 代表nginx的錯誤日志存放的位置
pid /var/run/nginx.pid; # nginx運行的標志
# 以上統稱為全局塊,
events { # events塊
worker_connections 1024; # worker_connections數值越大,nginx並發能力越強
}
http { # http塊
include /etc/nginx/mime.types; #include代表引入一個外部的文件 ->mime.types中放着大量的媒體類型
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf; # 引入了conf.d目錄下的以.conf為結尾的配置文件,在整個http塊中,主要關注這個就夠了,后期主要就是在編寫這個目錄下的配置文件
}
/etc/nginx/conf.d/defualt.conf
文件
這個文件是在http快中引入的,相當於在nginx.conf
文件中替換了http中的include /etc/nginx/conf.d/*.conf;
server {# server塊
listen 80; # 代表nginx監聽的端口號
listen [::]:80;
server_name localhost;
location / { # 代表nginx接收請求的ip
root /usr/share/nginx/html; # 將接收到的請求根據/usr/share/nginx/html去查找靜態資源
index index.html index.htm; # 默認去上述的路徑中找到index.html 或index.htm文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
真正要關注的是nginx.conf
中http塊中的include
中的/etc/nginx/conf.d/
中的所有的conf文件的server
塊
修改安裝nginx的docker-compose.yml文件
version: '3.6'
services:
nginx:
restart: always
image: daocloud.io/library/nginx:latest #鏡像
container_name: nginx # 容器名
ports:
- 80:80
volumes: #映射文件,
- /opt/docker_nginx/conf.d/:/etc/nginx/conf.d/ # 將配置文件映射到宿主機上...我個人是在/data/dockerdata/docker-compose-data/docker_nginx/conf.d
操作
exit # 退出nginx容器
#進入nginx的docker-compose.yml的路徑
docker-compose down
vim docker-compose.yml 修改docker-compose.yml
# 將上面的配置寫入
#保存文件
docker-compose build #重新構建
docker-compose up -d
# 啟動之后,在當前路徑下就創建了一個conf.d文件夾
現在訪問nginx頁面,會進不去,因為在conf.d文件夾下沒有默認配置,找不到對應的配置
創建默認conf配置
進入conf.d文件夾
運行 vi defualt.conf,編寫文件 # 默認沒有vim
server {# server塊
listen 80; # 代表nginx監聽的端口號
server_name localhost;
location / { # 代表nginx接收請求的ip
root /usr/share/nginx/html; # 將接收到的請求根據/usr/share/nginx/html去查找靜態資源
index index.html index.htm; # 默認去上述的路徑中找到index.html 或index.htm文件
}
}
保存文件
返回 docker-compose.yml文件夾
運行 docker-compose restart