欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Nginx實現(xiàn)分端口部署兩個或多個項目的教程

 更新時間:2023年10月23日 10:49:19   作者:think_mzs  
這篇文章主要為大家詳細介紹了Nginx實現(xiàn)分端口部署兩個或多個項目的相關(guān)教程,其中包含了反向代理配置,感興趣的小伙伴可以跟隨小編一起學習一下

一、部署Nginx

若讀者沒有部署安裝Nginx,則可以參考下面這篇文章進行安裝。

CentOS 7非編譯安裝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ù),其中第2listen 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頁面分別存放在project1project2文件夾中。

五、配置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)文章

最新評論