-----------------------------------------------安裝--------------------------------------------------------
yum -y install gcc pcre-devel zlib-devel openssl openssl-devel
nginx下載地址:https://nginx.org/download/
--解壓
tar -zxvf nginx-1.9.9.tar.gz 或者 tar -zxvf nginx-1.9.9.tar.gz -C /home/nginx
--進入nginx目錄
cd nginx-1.9.9
--配置
./configure --prefix=/usr/local/nginx
--make
make
make install
--檢測是否安裝成功到/usr/local/nginx目錄下
./sbin/nginx -t
---下面根據情況進行配置(可不用配置)
--開啓80端口:
firewall-cmd --add-port=80/tcp --permanent
#重啓防火牆
systemctl restart firewalld
--配置nginx開機自啓動
vim /etc/rc.d/rc.local
文件中添加 /usr/local/nginx/sbin/nginx
----------------------------------------------nginx常用操作---------------------------------------------------
到nginx的安裝目錄下
--快速停止或關閉Nginx:./nginx -s stop
--正常停止或關閉Nginx:./nginx -s quit
--配置文件修改重裝載命令:nginx -s reload
-------------------------------刪除nginx相關的所有配置(用於重裝nginx)-----------------------------------------
--停止nginx
service nginx stop
--刪除nginx自動啓動
chkconfig nginx off
--從源頭刪除Nginx (whereis nginx)
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
--再使用yum清理
yum remove nginx
---------------------------------------conf相關的配置介紹--------------------------------------------------------
目錄在nginx安裝目錄的conf/nginx.conf下
-- 重點配置
#test1 採用這種方式有助於後面增加負載,hash或其它負載方式
upstream test1sss {
server localhost:8090; # 對應當前本機tomcat1(測試)
}
#test2
upstream test2sss {
server localhost:8091; # 對應當前本機tomcat2(測試)
}
server {
# 監聽80端口
listen 80;
server_name www.pytest1.xxx.com pytest1.xxx.com;
access_log on;
#charset koi8-r;
charset utf-8;
# 不加有時是不會顯示目錄的
autoindex on;
#access_log logs/host.access.log main;
location /test1 {
proxy_pass http://test1sss/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#
location /test2 {
proxy_pass http://test2sss/;
proxy_set_header Host $host;
proxy_connect_timeout 5s;
}
location / {
root html;
index index.html index.htm;
}
# 此處訪問地址爲/home/dist1
location /dist1 {
alias /home/dist1/;
index index.html index.htm;
}
# 此處訪問地址爲/home/dist2/dist2
location /dist2 {
root /home/dist2/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
-----其它示例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
------------------------------logs下面的nginx pid被刪除的解決辦法-------------------------------------------
查詢nginx進程號並寫入logs下的pid文件
ps -ef | grep nginx
-- 在logs下創建文件
touch nginx.pid
-- 寫入文件
echo "34222" > nginx.pid
-- 重啓服務:
./nginx -s reload
在docker裏面安裝nginx配置起來個人覺得挺麻煩,所以一般情況下選擇提前下載好安裝包直接在linux下安裝的方式,文章僅供參考,具體安裝方式也可根據具體情況而定。