練手環(huán)境
阿里云Linux服務(wù)器 鏡像ID:centos_7_7_x64_20G_alibase_20191225.vhd
1.添加資源庫
在 CentOS 系統(tǒng)上安裝 Nginx 隘击,你得先去添加一個(gè)資源庫
EPEL 倉庫中有 Nginx 的安裝包锌云。如果你還沒有安裝過 EPEL郭变,可以通過運(yùn)行下面的命令來完成安裝
sudo yum install epel-release
上面代碼的意思是以
sudo
權(quán)限運(yùn)行安裝epel-release
拓巧,如果你當(dāng)前登錄的用戶不是 root涎永,則會(huì)提示你輸入密碼來運(yùn)行
2.安裝Nginx
sudo yum install nginx
提示OK就輸入y
3.啟動(dòng)Nginx
systemctl start nginx
systemctl status nginx
4.開啟端口80和443
如果你的服務(wù)器打開了防火墻思币,你需要運(yùn)行下面的命令,打開80和443端口羡微。
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
如果你的服務(wù)器是阿里云ECS谷饿,你還可以通過控制臺(tái)安全組,打開80和443端口妈倔,或者其他自定義端口博投。
具體操作路徑: 阿里云ECS服務(wù)器 -》 安全組 -》 配置規(guī)則 -》 安全組規(guī)則 -》 入方向 -》 添加安全組規(guī)則
端口范圍: 比如你要打開80端口,這里就填寫 80/80 盯蝴。
優(yōu)先級(jí): 優(yōu)先級(jí)可選范圍為1-100毅哗,默認(rèn)值為1听怕,即最高優(yōu)先級(jí)。
5.驗(yàn)證 Nginx 是否成功啟動(dòng)
在瀏覽器中打開 http://(公網(wǎng)ip)
虑绵,您將看到默認(rèn)的 Nginx 歡迎頁面尿瞭,類似于下圖所示:
6.配置nginx
nginx -t
當(dāng)你執(zhí)行 nginx -t 得時(shí)候,nginx會(huì)去測(cè)試你得配置文件得語法翅睛,并告訴你配置文件是否寫得正確声搁,同時(shí)也告訴了你配置文件得路徑
查看
/etc/nginx/nginx.conf
文件,內(nèi)容如下
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
最后一行可以看出nginx的配置都在conf.d
文件中捕发,而且是讀取改文件夾下所有的.conf
文件疏旨。所以我們可以在這個(gè)文件夾下隨意的創(chuàng)建配置文件,比如test.conf
扎酷,其內(nèi)容如下:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
index index.html index.htm index.jsp;
root /usr/local/tomcat/webapps/項(xiàng)目地址;
location / {
proxy_pass http://127.0.0.1:8080;
}
#靜態(tài)文件充石,nginx自己處理
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
nginx 通過以上的配置,把請(qǐng)求指向到本服務(wù)器上的 3000
端口的服務(wù)商
更多配置請(qǐng)?jiān)L問:
http://www.reibang.com/p/6e5c9095e350
https://www.cnblogs.com/dongye95/p/11096785.html#_label0_3 (推薦)