Nginx實現(xiàn)分端口部署兩個或多個項目的教程
一、部署Nginx
若讀者沒有部署安裝Nginx,則可以參考下面這篇文章進行安裝。
二、分析Nginx配置文件
通過上面的方法安裝的Nginx,其配置文件在/etc/nginx/
目錄下,如下圖所示。
其中nginx.conf
為Nginx的主要配置文件,在conf.d
文件夾中還存在著其他配置文件,通過nginx.conf
文件中的include語句導(dǎo)入至Nginx中。
nginx.conf
文件內(nèi)容如下所示。
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
,其內(nèi)容如下:
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; #} }
這一整個配置文件代表著一個服務(wù),其中第2
行listen 80
表示該服務(wù)監(jiān)聽80
端口。
服務(wù)的根目錄配置位于第8
至第11
行,其中root /usr/share/nginx/html;
表示服務(wù)所在的目錄。第10
行的 index index.html index.htm;
代表支持的首頁文件。
三、準備演示頁面
項目1的index.html
文件內(nèi)容如下。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>think</title> </head> <body> <h1>這是項目1首頁</h1> </body> </html>
項目2的index.html
文件內(nèi)容如下。
<!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
文件內(nèi)容如下所示。
server { listen 8080; location / { root /usr/share/nginx/html/project2; index index.html index.htm; } }
使用命令nginx -s reload
使得配置文件生效。
六、訪問測試
我的服務(wù)器ip為:192.168.0.55
,因此我訪問http://192.168.0.55
即可訪問到項目1。
訪問http://192.168.0.55:8080
即可訪問到項目2首頁。
注意:如果服務(wù)器沒有開啟8080
端口,那么直接訪問8080
防火墻將會被服務(wù)器的防火墻所攔截。因此,當發(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,因此這里設(shè)置成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
端口訪問服務(wù)器,該請求將會被重定向至服務(wù)器中的監(jiān)聽5000
端口的服務(wù)。
至此,使用Nginx發(fā)布兩個或者多個項目的教程已經(jīng)結(jié)束。若還有其他項目,則可以按照project2
的配置方式新建配置文件進行發(fā)布即可。
以上就是Nginx實現(xiàn)分端口部署兩個或多個項目的教程的詳細內(nèi)容,更多關(guān)于Nginx部署項目的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
nginx如何設(shè)置服務(wù)器響應(yīng)時間長短
本文主要介紹了nginx如何設(shè)置服務(wù)器響應(yīng)時間長短,主要介紹了兩種方法,具有一定的參考價值,感興趣的可以了解一下2023-09-09Nginx配置?location模塊實現(xiàn)路由(反向代理、重定向)功能
本文主要介紹了Nginx配置?location模塊實現(xiàn)路由(反向代理、重定向)功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁面打開速度)
這篇文章主要介紹了Nginx性能優(yōu)化之Gzip壓縮設(shè)置詳解(最大程度提高頁面打開速度),需要的朋友可以參考下2022-01-01詳解nginx?中l(wèi)ocation和?proxy_pass的匹配規(guī)則
location是Nginx中用來匹配客戶端請求URI的指令,決定如何處理特定路徑的請求,它定義了請求的路由規(guī)則,后續(xù)的配置(如?proxy_pass)會應(yīng)用在匹配的請求上,這篇文章主要介紹了nginxlocation和proxy_pass的匹配規(guī)則,需要的朋友可以參考下2025-04-04Nginx解決Http慢攻擊(Slow HTTP Attack)的方法
緩慢的HTTP拒絕服務(wù)攻擊是一種專門針對于Web的應(yīng)用層拒絕服務(wù)攻擊,本文給大家介紹了Nginx解決Http慢攻擊(Slow HTTP Attack)的方法,需要的朋友可以參考下2024-02-02