Nginx怎么部署服務(wù)

本篇內(nèi)容介紹了“Nginx怎么部署服務(wù)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供秦安網(wǎng)站建設(shè)、秦安做網(wǎng)站、秦安網(wǎng)站設(shè)計(jì)、秦安網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、秦安企業(yè)網(wǎng)站模板建站服務(wù),十多年秦安做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

一、查看配置

上一篇我們將 nginx 安裝在 /usr/local/nginx 目錄下,其默認(rèn)的配置文件都放在這個(gè)目錄的 conf 目錄下,而主配置文件 nginx.conf 也在其中,后續(xù)部署應(yīng)用時(shí)僅對(duì) nginx 的配置文件進(jìn)行相應(yīng)的修改,所以我們先大致介紹一下該配置文件的結(jié)構(gòu)。

cd /usr/local/nginx/conf/

1.1、原始版配置

#user  nobody;
worker_processes  1; #工作進(jìn)程:數(shù)目。根據(jù)硬件調(diào)整,通常等于cpu數(shù)量或者2倍cpu數(shù)量。
 
#錯(cuò)誤日志存放路徑
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid; # nginx進(jìn)程pid存放路徑
 
 
events {
    worker_connections  1024; # 工作進(jìn)程的最大連接數(shù)量
}
 
 
http {
    include       mime.types; #指定mime類型,由mime.type來(lái)定義
    default_type  application/octet-stream;
 
    # 日志格式設(shè)置
    #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  logs/access.log  main; #用log_format指令設(shè)置日志格式后,需要用access_log來(lái)指定日志文件存放路徑
					
    sendfile        on; #指定nginx是否調(diào)用sendfile函數(shù)來(lái)輸出文件,對(duì)于普通應(yīng)用,必須設(shè)置on。
			如果用來(lái)進(jìn)行下載等應(yīng)用磁盤io重負(fù)載應(yīng)用,可設(shè)著off,以平衡磁盤與網(wǎng)絡(luò)io處理速度,降低系統(tǒng)uptime。
    #tcp_nopush     on; #此選項(xiàng)允許或禁止使用socket的TCP_CORK的選項(xiàng),此選項(xiàng)僅在sendfile的時(shí)候使用
 
    #keepalive_timeout  0;  #keepalive超時(shí)時(shí)間
    keepalive_timeout  65;
 
    #gzip  on; #開(kāi)啟gzip壓縮服務(wù)
 
    #虛擬主機(jī)
    server {
        listen       80;  #配置監(jiān)聽(tīng)端口號(hào)
        server_name  localhost; #配置訪問(wèn)域名,域名可以有多個(gè),用空格隔開(kāi)
 
        #charset koi8-r; #字符集設(shè)置
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
        #錯(cuò)誤跳轉(zhuǎn)頁(yè)
        #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   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$ { #請(qǐng)求的url過(guò)濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。
        #    root           html; #根目錄
        #    fastcgi_pass   127.0.0.1:9000; #請(qǐng)求轉(zhuǎn)向定義的服務(wù)器列表
        #    fastcgi_index  index.php; # 如果請(qǐng)求的Fastcgi_index URI是以 / 結(jié)束的, 該指令設(shè)置的文件會(huì)被附加到URI的后面并保存在變量$fastcig_script_name中
        #    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;
        #}
    }
 
 
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
 
    # HTTPS server
    #
    #server {
    #    listen       443 ssl;  #監(jiān)聽(tīng)端口
    #    server_name  localhost; #域名
 
    #    ssl_certificate      cert.pem; #證書位置
    #    ssl_certificate_key  cert.key; #私鑰位置
 
    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m; 
 
    #    ssl_ciphers  HIGH:!aNULL:!MD5; #密碼加密方式
    #    ssl_prefer_server_ciphers  on; # ssl_prefer_server_ciphers  on; #
 
 
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
 
}

1.2、精簡(jiǎn)后配置

# 開(kāi)頭的表示注釋內(nèi)容,我們?nèi)サ羲幸?# 開(kāi)頭的段落,精簡(jiǎn)之后的內(nèi)容如下:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

二、認(rèn)識(shí)配置

2.1、全局塊

從配置文件開(kāi)始到 events 塊之間的內(nèi)容,主要會(huì)設(shè)置一些影響nginx 服務(wù)器整體運(yùn)行的配置指令,主要包括配置運(yùn)行 Nginx 服務(wù)器的用戶(組)、允許生成的 worker process 數(shù),進(jìn)程 PID 存放路徑、日志存放路徑和類型以及配置文件的引入等。

比如上面第一行配置的:

worker_processes  1;

這是 Nginx 服務(wù)器并發(fā)處理服務(wù)的關(guān)鍵配置,worker_processes 值越大,可以支持的并發(fā)處理量也越多,但是會(huì)受到硬件、軟件等設(shè)備的制約,這個(gè)后面會(huì)詳細(xì)介紹。

2.2、events塊

比如上面的配置:

events {    worker_connections  1024; }

events 塊涉及的指令主要影響 Nginx 服務(wù)器與用戶的網(wǎng)絡(luò)連接,常用的設(shè)置包括是否開(kāi)啟對(duì)多 work process 下的網(wǎng)絡(luò)連接進(jìn)行序列化,是否允許同時(shí)接收多個(gè)網(wǎng)絡(luò)連接,選取哪種事件驅(qū)動(dòng)模型來(lái)處理連接請(qǐng)求,每個(gè) word process 可以同時(shí)支持的最大連接數(shù)等。

上述例子就表示每個(gè) work process 支持的最大連接數(shù)為 1024.

這部分的配置對(duì) Nginx 的性能影響較大,在實(shí)際中應(yīng)該靈活配置。

2.3、http塊

http {
    include       mime.types;
    default_type  application/octet-stream;


    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }

}

這算是 Nginx 服務(wù)器配置中最頻繁的部分,代理、緩存和日志定義等絕大多數(shù)功能和第三方模塊的配置都在這里。

需要注意的是:http 塊也可以包括 http全局塊server 塊。

①、http 全局塊

http全局塊配置的指令包括文件引入、MIME-TYPE 定義、日志自定義、連接超時(shí)時(shí)間、單鏈接請(qǐng)求數(shù)上限等。

②、server 塊

這塊和虛擬主機(jī)有密切關(guān)系,虛擬主機(jī)從用戶角度看,和一臺(tái)獨(dú)立的硬件主機(jī)是完全一樣的,該技術(shù)的產(chǎn)生是為了節(jié)省互聯(lián)網(wǎng)服務(wù)器硬件成本。后面會(huì)詳細(xì)介紹虛擬主機(jī)的概念。

每個(gè) http 塊可以包括多個(gè) server 塊,而每個(gè) server 塊就相當(dāng)于一個(gè)虛擬主機(jī)。

而每個(gè) server 塊也分為全局 server 塊,以及可以同時(shí)包含多個(gè) locaton 塊。

1、全局 server 塊

最常見(jiàn)的配置是本虛擬機(jī)主機(jī)的監(jiān)聽(tīng)配置和本虛擬主機(jī)的名稱或IP配置。

2、location 塊

一個(gè) server 塊可以配置多個(gè) location 塊。

這塊的主要作用是基于 Nginx 服務(wù)器接收到的請(qǐng)求字符串(例如 server_name/uri-string),對(duì)虛擬主機(jī)名稱(也可以是IP別名)之外的字符串(例如 前面的 /uri-string)進(jìn)行匹配,對(duì)特定的請(qǐng)求進(jìn)行處理。地址定向、數(shù)據(jù)緩存和應(yīng)答控制等功能,還有許多第三方模塊的配置也在這里進(jìn)行。

三、部署前端

3.1、一般靜態(tài)資源

server {
        listen       80;
        server_name dianjiu.co www.dianjiu.co;

        location / {
            root /app/web/dianjiu_co;
            index  index.html index.htm;
        }
    }

3.2、vue代理資源

	server {
        listen       80;
        server_name dianjiu.co www.dianjiu.co;

        location / {
            root /app/web/dianjiu_co;
            index  index.html index.htm;
            ## 解決刷新頁(yè)面變成404問(wèn)題的代碼
            try_files $uri $uri/ /index.html;
        }
        location /api {
             ## 重寫,轉(zhuǎn)
             發(fā)前,將url中的某些參數(shù)進(jìn)行過(guò)濾
             rewrite  ^/api/(.*)$ /$1 break;
             ## 代理轉(zhuǎn)發(fā)地址,具體的 host 和 post 自己指定
             proxy_pass http://47.11.59.245:18181;
        }
    }

四、部署后端

4.1、代理服務(wù)端口

server {
        listen 80;   
        server_name login.dianjiu.co;

        location / {
            proxy_pass http://127.0.0.1:8080;
            index  index.html index.htm index.jsp;
        }
    }

“Nginx怎么部署服務(wù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

文章名稱:Nginx怎么部署服務(wù)
轉(zhuǎn)載注明:http://muchs.cn/article32/pidgsc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、外貿(mào)建站網(wǎng)站排名、網(wǎng)頁(yè)設(shè)計(jì)公司App設(shè)計(jì)、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)