Nginx實現(xiàn)分端口部署兩個或多個項目的教程
一、部署Nginx
若讀者沒有部署安裝Nginx,則可以參考下面這篇文章進行安裝。
二、分析Nginx配置文件
通過上面的方法安裝的Nginx,其配置文件在/etc/nginx/目錄下,如下圖所示。

其中nginx.conf為Nginx的主要配置文件,在conf.d文件夾中還存在著其他配置文件,通過nginx.conf文件中的include語句導入至Nginx中。
nginx.conf文件內容如下所示。
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
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;
}
第31行語句表示將/etc/nginx/conf.d/目錄下的所有.conf文件包含至配置文件中。因此我們可以在conf.d目錄下創(chuàng)建我們項目的配置文件。
在conf.d目錄下默認擁有一份配置文件:default.conf,其內容如下:
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/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 /usr/share/nginx/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;
#}
}
這一整個配置文件代表著一個服務,其中第2行listen 80表示該服務監(jiān)聽80端口。
服務的根目錄配置位于第8至第11行,其中root /usr/share/nginx/html;表示服務所在的目錄。第10行的 index index.html index.htm;代表支持的首頁文件。
三、準備演示頁面
項目1的index.html文件內容如下。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>think</title> </head> <body> <h1>這是項目1首頁</h1> </body> </html>
項目2的index.html文件內容如下。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>think</title> </head> <body> <h1>這是項目2首頁</h1> </body> </html>
以上兩個index.html文件分別代表兩個WEB項目作為演示。
四、上傳項目
使用Xftp將兩個項目上傳至Nginx的web目錄下。

兩個index.html頁面分別存放在project1和project2文件夾中。
五、配置Nginx
項目1我們使用80端口進行發(fā)布,因此我們需要修改default.conf文件中的web目錄為/usr/share/nginx/html/project1,如下圖所示。

項目2我們使用8080端口進行發(fā)布,因此我們需要在conf.d目錄中新建一個配置文件:project2.conf。

配置文件名稱可以自己定義,但必須是.conf文件!
project2.conf文件內容如下所示。
server {
listen 8080;
location / {
root /usr/share/nginx/html/project2;
index index.html index.htm;
}
}
使用命令nginx -s reload使得配置文件生效。
六、訪問測試
我的服務器ip為:192.168.0.55,因此我訪問http://192.168.0.55即可訪問到項目1。

訪問http://192.168.0.55:8080即可訪問到項目2首頁。

注意:如果服務器沒有開啟8080端口,那么直接訪問8080防火墻將會被服務器的防火墻所攔截。因此,當發(fā)現(xiàn)訪問項目2時出現(xiàn)無法訪問,則依次執(zhí)行以下命令開啟8080端口。
防火墻開啟8080端口
firewall-cmd --zone=public --add-port=5672/tcp --permanent
使防火墻配置立即生效。
firewall-cmd --reload
七、反向代理配置
若是需要進行反向代理,則是需要使用如下配置。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
#開啟代理功能,因為.net core的默認端口為5000,因此這里設置成5000,如遇變化則該處端口配置也要變化
proxy_pass http://localhost:5000;
}
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 /usr/share/nginx/html;
}
}
方向代理配置完成之后,通過80端口訪問服務器,該請求將會被重定向至服務器中的監(jiān)聽5000端口的服務。
至此,使用Nginx發(fā)布兩個或者多個項目的教程已經(jīng)結束。若還有其他項目,則可以按照project2的配置方式新建配置文件進行發(fā)布即可。
以上就是Nginx實現(xiàn)分端口部署兩個或多個項目的教程的詳細內容,更多關于Nginx部署項目的資料請關注腳本之家其它相關文章!
相關文章
Nginx配置?location模塊實現(xiàn)路由(反向代理、重定向)功能
本文主要介紹了Nginx配置?location模塊實現(xiàn)路由(反向代理、重定向)功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04
Nginx性能優(yōu)化之Gzip壓縮設置詳解(最大程度提高頁面打開速度)
這篇文章主要介紹了Nginx性能優(yōu)化之Gzip壓縮設置詳解(最大程度提高頁面打開速度),需要的朋友可以參考下2022-01-01
詳解nginx?中l(wèi)ocation和?proxy_pass的匹配規(guī)則
location是Nginx中用來匹配客戶端請求URI的指令,決定如何處理特定路徑的請求,它定義了請求的路由規(guī)則,后續(xù)的配置(如?proxy_pass)會應用在匹配的請求上,這篇文章主要介紹了nginxlocation和proxy_pass的匹配規(guī)則,需要的朋友可以參考下2025-04-04
Nginx解決Http慢攻擊(Slow HTTP Attack)的方法
緩慢的HTTP拒絕服務攻擊是一種專門針對于Web的應用層拒絕服務攻擊,本文給大家介紹了Nginx解決Http慢攻擊(Slow HTTP Attack)的方法,需要的朋友可以參考下2024-02-02

