Nginx安裝
Nginx下載
官網(wǎng)下載:http://nginx.org/en/download.html
或者直接在linux執(zhí)行命令:wget http://nginx.org/download/nginx-1.12.2.tar.gz
這里下載的版本是1.12.2
安裝步驟
# 安裝依賴
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
# 解壓縮
tar -zxvf linux-nginx-1.12.2.tar.gz
cd nginx-1.12.2/
# 執(zhí)行配置
./configure
# 編譯安裝(默認(rèn)安裝在/usr/local/nginx)
make
make install
防火墻配置
nginx默認(rèn)監(jiān)聽(tīng)80端口手蝎,如果未關(guān)閉防火墻需要配置iptables規(guī)則開(kāi)放80端口(以centos6為例)活喊。
編輯配置文件:vim /etc/sysconfig/iptables
在文件中間添加iptables規(guī)則
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
重啟防火墻:service iptables restart
或者關(guān)閉iptables規(guī)則:iptables -F && iptables -t nat -F
Nginx驗(yàn)證
nginx主配置文件:/usr/local/nginx/conf/nginx.conf
nginx日志文件:/usr/local/nginx/logs/access.log
啟動(dòng)Nginx:/usr/local/nginx/sbin/nginx
然后直接訪問(wèn)ip地址搜囱,比如:http://192.168.0.110/以舒,如果能看到如下Nginx主頁(yè)說(shuō)明安裝ok。
Nginx常用命令
查找安裝路徑:
whereis nginx
進(jìn)入sbin目錄,可以看到有一個(gè)可執(zhí)行文件nginx榛斯,直接./執(zhí)行就OK了观游。
測(cè)試配置文件:{Nginx}/sbin/nginx
停止命令:{Nginx}/sbin/nginx -s reload
查看進(jìn)程命令:ps -ef | grep nginx
平滑重啟:kill -HUP [Nginx主進(jìn)程號(hào)(即ps命令查到的PID)]
【部署問(wèn)題】解決Nginx: [error] open() "/usr/local/Nginx/logs/Nginx.pid" failed(2:No such file or directory)
問(wèn)題:環(huán)境問(wèn)題
解決方法:
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[Nginx 配置多站點(diǎn)vhost]
假設(shè)你想在Linux Nginx中用不同的域名訪問(wèn)不同的目錄,這時(shí)就要配置多個(gè)vhost驮俗,具體配置如下懂缕,假設(shè)網(wǎng)站根目錄設(shè)定在/var/www/
1、在/var/www/下新建兩個(gè)目錄
/var/www/ushark.net
/var/www/ushark.wang
2王凑、編輯/etc/nginx/nginx.conf
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf; √赂獭#主要是加入此行,如有則忽略
}
3索烹、在/etc/nginx/conf.d下新建兩個(gè)conf文件
/etc/nginx/conf.d/ushark.net.conf
/etc/nginx/conf.d/ushark.wang.conf
4工碾、復(fù)制如下配置信息到兩個(gè)文件中,只要修改紅色部分內(nèi)容 !!! server_name與root保持一致即目錄和域名一一對(duì)應(yīng) !!!
server {
listen 80;
server_name www.ushark.net;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
root /var/www/ushark.net/;
if (!-e $request_filename){ “傩铡Tǘ睿 rewrite可根據(jù)網(wǎng)站需要增刪
rewrite ^/(.*) /index.php last;
}
location / {
index index.php 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 /var/www/ushark.net/;
}
# 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$ {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
5、重啟Nginx
systemctl restart nginx
6垒拢、 編輯/etc/hosts !!! 核心步驟 !!!
[root@bogon ~]# vi 127.0.0.1 localhost.localdomain localhost
::1 localhost6.localdomain6 localhost6
127.0.0.1 www.ushark.net
127.0.0.1 www.ushark.wang
3.因?yàn)檫@里使用的是虛擬機(jī)旬迹,所以需要配置host文件,注意host配置的應(yīng)該是我們使用的客戶端也就是瀏覽器所在的主機(jī)host子库,這里使用的是windows系統(tǒng)訪問(wèn)舱权,所以應(yīng)該在windows下配置host矗晃,位置:C://Windows/System32/drivers/etc
編輯hosts文件仑嗅,在文件末尾添加內(nèi)容(虛擬機(jī)ip + 需要映射的域名):
192.168.0.110 http://www.silly.com
配置好了host后,打開(kāi)cmd张症,執(zhí)行ping命令驗(yàn)證是否生效仓技。
- 重啟Nginx:
/usr/local/nginx/sbin/nginx -s reload
然后啟動(dòng)tomcat:${tomcat}/bin/startup.sh
5.訪問(wèn)域名:http://www.silly.com/,就可以訪問(wèn)到tomcat的主頁(yè)了俗他。
image.png
總結(jié):整個(gè)過(guò)程就是當(dāng)我們?cè)L問(wèn)域名時(shí)脖捻,首先根據(jù)host配置映射到了一個(gè)IP地址,也就是訪問(wèn)到了虛擬機(jī)兆衅,不加端口默認(rèn)訪問(wèn)的就是80端口地沮,也就是訪問(wèn)到了虛擬機(jī)的Nginx,在Nginx配置中對(duì)該域名進(jìn)行了反向代理羡亩,代理到了本機(jī)的8080端口服務(wù)摩疑,所以最終就訪問(wèn)到了虛擬機(jī)的Tomcat服務(wù)器。
更新
1畏铆,查看指定端口進(jìn)程
netstat -lnp|grep 80 # 查看占用80端口的進(jìn)程信息,主要看PID
ps [pid] #根據(jù)PID查看進(jìn)程的詳細(xì)信息;
kill -9 [PID] # 根據(jù)PID殺死進(jìn)程
2雷袋,重新啟動(dòng)jar包
java -jar spring-boot-base-0.0.2-SNAPSHOT.jar