闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

Posted 澶ф暟鎹ⅵ鎯冲

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢相关的知识,希望对你有一定的参考价值。

鍓嶈█

        閫氳繃鍓嶉潰涓€绡囨枃绔?馃憠)鐨勫涔狅紝鎴戜滑宸茬粡鐭ラ亾浜哊ginx鏄竴娆鹃珮鎬ц兘鐨勪唬鐞嗘湇鍔″櫒杞欢锛屾敮鎸?span>姝e悜浠g悊鍜?span>鍙嶅悜浠g悊銆傚悓鏃讹紝Nginx杩樿兘瀹炵幇鏈嶅姟鍣ㄧ殑闆嗙兢鍜岃礋杞藉潎琛★紝鏀寔鎻掍欢寮忓紑鍙戙€傞櫎姝や箣澶栵紝Nginx杩樻湁寰堝鍗佸垎寮哄ぇ鐨勫姛鑳斤紝濡傚瀹㈡埛绔?span>闄愭祦锛?span>缂撳瓨锛?span>鍔ㄦ€侀粦鐧藉悕鍗?/strong>鍜屽疄鐜?span>鐏板害鍙戝竷绛夈€傛湰绡囨垜浠氨鏉ヤ粙缁峃ginx鐨勫畨瑁呭拰閰嶇疆銆?/p>

Nginx鐨勫畨瑁?/span>

        瀹夎鍜岄厤缃甆ginx涔嬪墠锛屾垜浠渶瑕佸厛瀹夎涓€浜汵ginx鐩稿叧鐨勪緷璧栥€?/p>

  • gcc
  • pcre
  • openssl
  • zlib

gcc

        瀹夎 nginx 闇€瑕佸厛灏嗗畼缃戜笅杞界殑婧愮爜杩涜缂栬瘧锛岀紪璇戜緷璧?gcc 鐜锛屽鏋滄病鏈?gcc 鐜锛屽垯闇€瑕佸畨瑁咃細

yum install gcc-c++

PCRE

        PCRE(Perl Compatible Regular Expressions) 鏄竴涓狿erl搴擄紝鍖呮嫭 perl 鍏煎鐨?span>姝e垯琛ㄨ揪寮?/strong>搴撱€俷ginx 鐨?http 妯″潡浣跨敤 pcre 鏉ヨВ鏋愭鍒欒〃杈惧紡锛屾墍浠ラ渶瑕佸湪 linux 涓婂畨瑁?pcre 搴撱€俻cre-devel 鏄娇鐢?pcre 寮€鍙戠殑涓€涓簩娆″紑鍙戝簱銆俷ginx涔熼渶瑕佹搴撱€傚懡浠わ細

yum install -y pcre pcre-devel

OPENSSL

        OpenSSL 鏄竴涓己澶х殑瀹夊叏濂楁帴瀛楀眰瀵嗙爜搴擄紝鍥婃嫭涓昏鐨勫瘑鐮佺畻娉曘€佸父鐢ㄧ殑瀵嗛挜鍜岃瘉涔﹀皝瑁呯鐞嗗姛鑳藉強 SSL 鍗忚锛屽苟鎻愪緵涓板瘜鐨勫簲鐢ㄧ▼搴忎緵娴嬭瘯鎴栧叾瀹冪洰鐨勪娇鐢ㄣ€?/p>

        Nginx 涓嶄粎鏀寔 http 鍗忚锛岃繕鏀寔 https锛堝嵆鍦╯sl鍗忚涓婁紶杈揾ttp锛夛紝鎵€浠ラ渶瑕佸湪 Centos 瀹夎 OpenSSL 搴撱€?/p>

yum install -y openssl openssl-devel

zlib

        褰揘ginx闇€瑕佹敮鎸佸帇缂╁姛鑳芥椂锛屽闇€瑕佸涓婁紶鐨勫浘鐗囧拰浼犺緭鐨勬暟鎹繘琛屽帇缂╁鐞嗭紝灏遍渶瑕佷娇鐢ㄥ埌zlib銆倆lib 搴撴彁渚涗簡寰堝绉嶅帇缂╁拰瑙e帇缂╃殑鏂瑰紡锛?鎵€浠ラ渶瑕佸湪 Centos 涓婂畨瑁?zlib 搴撱€?/p>

yum install -y zlib zlib-devel

瀹夎Nginx

        涓嬭浇nginx锛屼袱绉嶆柟寮?/p>

  1. 鐩存帴涓嬭浇 .tar.gz瀹夎鍖咃紝鍦板潃锛?

        https://nginx.org/en/download.html

  1. 浣跨敤 wget鍛戒护涓嬭浇锛? 鎺ㄨ崘锛夈€傜‘淇濈郴缁熷凡缁忓畨瑁呬簡wget锛屽鏋滄病鏈夊畨瑁咃紝鎵ц yum install wget 瀹夎銆?
wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

        渚濈劧鏄洿鎺ュ懡浠わ細

# 瑙e帇
tar -zxvf nginx-1.12.2.tar.gz
# 杩涘叆瑙e帇鐩綍
cd nginx-1.12.2

        寮€濮嬮厤缃細

        鍏跺疄鍦?nginx-1.12.0 鐗堟湰涓綘灏变笉闇€瑕佸幓閰嶇疆鐩稿叧涓滆タ锛岄粯璁ゅ氨鍙互浜嗐€傚綋鐒讹紝濡傛灉浣犺鑷繁閰嶇疆鐩綍涔熸槸鍙互鐨勩€?/p>

        浣跨敤榛樿閰嶇疆

./configure

        缂栬瘧瀹夎

make && make install

        缂栬瘧瀹屾垚涔嬪悗锛?鎴戜滑鍚姩Nginx

cd /usr/local/nginx/sbin
./nginx 

        鍙互鏌ョ湅Nginx鐨勮繘绋?/p>

[root@localhost conf]#  ps aux|grep nginx
root     108255  0.0  0.0  20540   600 ?        Ss   15:09   0:00 nginx: master process ./nginx
nobody   108256  0.0  0.1  23068  1608 ?        S    15:09   0:00 nginx: worker process
root     108636  0.0  0.0 112824   980 pts/1    R+   16:06   0:00 grep --color=auto nginx

        鍦ㄦ祻瑙堝櫒涓緭鍏P锛屼緥濡?code class="mq-90">192.168.100.128 鍙互鐪嬪埌杩欐牱鐨勯〉闈紝璇存槑鎴戜滑鐨凬ginx灏卞惎鍔ㄦ垚鍔熶簡銆?nbsp;       闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

Nginx鐨勯粯璁ゅ畨瑁呰矾寰勪负锛?span>/usr/local/nginx/ 锛屽鏋滄兂瑕佹洿鏀硅矾寰勶紝闇€瑕侀噰鐢ㄨ蒋閾炬帴鐨勬柟寮忚繘琛岃缃€?/p>

        鎴戜滑鎵撳紑 Nginx 鐨勯厤缃枃浠?/p>

vi /usr/local/nginx/conf/nginx.conf

        鍙互鍙戠幇榛樿 listen 鐨勫€兼槸 80闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢        璇存槑鎴戜滑鐜板湪榛樿鐨勮闂鍙f槸锛?code class="mq-101">80 锛屾垜浠啀閫氳繃ip + 绔彛璁块棶涔熸槸涓€鏍风殑銆?/p>

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢        浣嗘垜浠幇鍦ㄧ敤鐨勬槸Linux绯荤粺锛岄粯璁ゆ儏鍐典笅鏈夐槻鐏銆傛垜浠琋ginx鐨勮闂鍙f槸 80锛岄粯璁ゆ槸璁块棶涓嶄簡鐨勶紝涔熷氨鏄垜浠細鐪嬩笉鍒扮晫闈€傚洜姝ゆ垜浠渶瑕佸湪闃茬伀澧欎腑璁剧疆寮€鏀剧殑绔彛銆?/p>

# 鏌ョ湅寮€鏀剧殑绔彛
firewall-cmd --list-all
闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢
# 璁剧疆寮€鏀剧殑绔彛
[root@localhost conf]# firewall-cmd --add-service=http --permanent
success
[root@localhost conf]# firewall-cmd --add-port=80/tcp --permanent
success
# 閲嶅惎闃茬伀澧?/span>
[root@localhost conf]# firewall-cmd --reload
success

        鎴戜滑鍐嶆鏌ョ湅寮€鏀剧殑绔彛锛屽氨鍙互鍙戠幇 80 绔彛宸茬粡琚坊鍔犺繘鏉ヤ簡銆?/p>

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

Nginx涓父鐢ㄥ懡浠?/span>

        浣跨敤 Nginx 鎿嶄綔鍛戒护鍓嶆彁鏉′欢锛?span class="mq-128">蹇呴』杩涘叆 nginx 鐨?鐩綍 /usr/local/nginx/sbin

cd /usr/local/nginx/sbin/

        鏌ョ湅鐗堟湰鍙?/strong>

[root@localhost sbin]# ./nginx -v
nginx version: nginx/1.12.2

        鏌ユ壘瀹夎璺緞锛?/strong>

[root@localhost sbin]# whereis nginx
nginx: /usr/local/nginx

        鍚姩锛屽仠姝紝閲嶅姞杞?/strong>

 cd /usr/local/nginx/sbin/
 ./nginx 
 ./nginx -s stop   # 鍚姩
 ./nginx -s quit   # 閫€鍑?/span>
 ./nginx -s reload   # 閲嶆柊鍔犺浇(閰嶇疆鏂囦欢閲嶆柊鍔犺浇锛屾垜浠氨鏃犻渶閲嶅惎鐢熸晥)

Nginx鐨勯厤缃?/span>

        Nginx 瀹夎鐩綍涓嬶紝鍏堕粯璁ょ殑閰嶇疆鏂囦欢閮芥斁鍦ㄨ繖涓洰褰曠殑 conf 鐩綍涓嬶紝鑰屼富閰嶇疆鏂囦欢 nginx.conf涔熷湪鍏朵腑锛屽悗缁 nginx 鐨勪娇鐢ㄥ熀鏈笂閮芥槸瀵规閰嶇疆鏂囦欢杩涜鐩稿簲鐨勪慨鏀广€?/p>

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢        鎵撳紑閰嶇疆鏂囦欢锛屾垜浠彲浠ュ緢鏄庢樉鐨勫皢 nginx.conf 閰嶇疆鏂囦欢鍒嗕负涓夐儴鍒嗭細

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

绗竴閮ㄥ垎锛氬叏灞€鍧?span class="mq-167">

        浠庨厤缃枃浠跺紑濮嬪埌 events 鍧椾箣闂寸殑鍐呭锛屼富瑕佷細璁剧疆涓€浜涘奖鍝?nginx 鏈嶅姟鍣ㄦ暣浣撹繍琛岀殑閰嶇疆鎸囦护锛屼富瑕佸寘鎷厤缃繍琛?Nginx 鏈嶅姟鍣ㄧ殑鐢ㄦ埛锛堢粍锛夈€佸厑璁哥敓鎴愮殑 worker process 鏁帮紝杩涚▼ PID 瀛樻斁璺緞銆佹棩蹇楀瓨鏀捐矾寰勫拰绫诲瀷浠ュ強閰嶇疆鏂囦欢鐨勫紩鍏ョ瓑銆?/p>

        姣斿涓婇潰绗竴琛岄厤缃殑锛?/p>

worker_processes  1;

        杩欐槸 Nginx 鏈嶅姟鍣?span>骞跺彂澶勭悊鏈嶅姟鐨勫叧閿厤缃?/strong>锛?span class="mq-174">worker_processes 鍊艰秺澶э紝鍙互鏀寔鐨勫苟鍙戝鐞嗛噺涔熻秺澶氾紝浣嗘槸浼氬彈鍒扮‖浠躲€佽蒋浠剁瓑璁惧鐨勫埗绾?/span>

绗簩閮ㄥ垎锛歟vents鍧?span class="mq-177">

        姣斿涓婇潰鐨勯厤缃細

events {
    worker_connections  1024;
}

        events 鍧楁秹鍙婄殑鎸囦护涓昏褰卞搷 Nginx 鏈嶅姟鍣ㄤ笌鐢ㄦ埛鐨勭綉缁滆繛鎺?/strong>锛屽父鐢ㄧ殑璁剧疆鍖呮嫭鏄惁寮€鍚澶?work process 涓嬬殑缃戠粶杩炴帴杩涜搴忓垪鍖栵紝鏄惁鍏佽鍚屾椂鎺ユ敹澶氫釜缃戠粶杩炴帴锛岄€夊彇鍝浜嬩欢椹卞姩妯″瀷鏉ュ鐞嗚繛鎺ヨ姹傦紝姣忎釜 word process 鍙互鍚屾椂鏀寔鐨勬渶澶ц繛鎺ユ暟绛夈€?/p>

        涓婅堪渚嬪瓙灏辫〃绀烘瘡涓?work process 鏀寔鐨?span>鏈€澶ц繛鎺ユ暟涓?1024 銆?span class="mq-184">杩欓儴鍒嗙殑閰嶇疆瀵?Nginx 鐨勬€ц兘褰卞搷杈冨ぇ锛屽湪瀹為檯涓簲璇ョ伒娲婚厤缃?/span>銆?/p>

绗笁閮ㄥ垎锛歨ttp 鍧?span class="mq-187">

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   html;
        }

        杩欑畻鏄?Nginx 鏈嶅姟鍣ㄩ厤缃腑鏈€棰戠箒鐨勯儴鍒嗭紝浠g悊銆佺紦瀛樺拰鏃ュ織瀹氫箟绛夌粷澶у鏁板姛鑳藉拰绗笁鏂规ā鍧楃殑閰嶇疆閮藉湪杩欓噷銆?/span>鎴戜滑鍚庨潰瑕佷粙缁嶇殑鍙嶅悜浠g悊锛岃礋杞藉潎琛★紝鍔ㄩ潤鍒嗙閮芥槸鍦ㄨ繖閲岄厤缃殑銆?/p>

        闇€瑕佹敞鎰忕殑鏄細http 鍧椾篃鍙互鍖呮嫭 http 鍏ㄥ眬鍧椼€乻erver 鍧?/strong>銆?/p>

  • http 鍏ㄥ眬鍧?

        http 鍏ㄥ眬鍧?/strong>閰嶇疆鐨勬寚浠ゅ寘鎷?span>鏂囦欢寮曞叆銆丮IME-TYPE 瀹氫箟銆佹棩蹇楄嚜瀹氫箟銆佽繛鎺ヨ秴鏃舵椂闂淬€佸崟閾炬帴璇锋眰鏁颁笂闄?/strong>绛夈€?/p>

include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
  • server 鍧?
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   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   html;
        }

        杩欏潡鍜?span>铏氭嫙涓绘満鏈夊瘑鍒囧叧绯伙紝铏氭嫙涓绘満浠庣敤鎴疯搴︾湅锛屽拰涓€鍙扮嫭绔嬬殑纭欢涓绘満鏄畬鍏ㄤ竴鏍风殑锛岃鎶€鏈殑浜х敓鏄负浜?span>鑺傜渷浜掕仈缃戞湇鍔″櫒纭欢鎴愭湰銆?/p>

        姣忎釜 http 鍧楀彲浠ュ寘鎷涓?server 鍧楋紝鑰屾瘡涓?server 鍧楀氨鐩稿綋浜庝竴涓櫄鎷熶富鏈?/span>銆?/p>

        鑰屾瘡涓?server 鍧椾篃鍒嗕负鍏ㄥ眬 server 鍧楋紝浠ュ強鍙互鍚屾椂鍖呭惈澶氫釜 locaton 鍧椼€傛垜浠悗杈硅瑙ginx瀹炰緥锛屼篃涓昏鏄湪杩欓儴鍒嗚繘琛屼慨鏀广€?/p>

宸ㄤ汉鐨勮偐鑶€

https://www.bilibili.com/video/BV1zJ411w7SV?p=7

褰╄泲

        鏈夊垢鏀堕泦杩囦竴浠藉叧浜?span>Nginx鐨勯潰璇曚笓棰?/strong>锛岄渶瑕佺殑鏈嬪弸鍙互鍏虫敞銆愮尶浜鸿弻銆戝悗鍙板洖澶嶁€淣ginx鈥濊繘琛岃幏鍙栥€?/p>

闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢

灏忕粨

        鏈瘒鏂囩珷鍒拌繖閲屽氨缁撴潫浜嗭紝涓嶇煡閬撳皬浼欎即浠兘鎺屾彙寰楁€庝箞鏍峰憿锛佷笅涓€鏈燂紝鎴戜滑瑕佸涔燦ginx鐨勯厤缃疄渚嬶紝鍖呭惈鍙嶅悜浠g悊锛岃礋杞藉潎琛★紝鍔ㄩ潤鍒嗙绛夌瓑鏁鏈熷緟锛?span>浣犵煡閬撶殑瓒婂锛屼綘涓嶇煡閬撶殑涔熻秺澶?/strong>锛屾垜鏄疉lice锛屾垜浠笅涓€鏈熻锛?/p>

         涓€閿笁杩烇紝鍏绘垚涔犳儻~

鏂囩珷鎸佺画鏇存柊锛屽彲浠ュ井淇℃悳涓€鎼溿€?鐚夸汉鑿?銆嶇涓€鏃堕棿闃呰锛屾€濈淮瀵煎浘锛屽ぇ鏁版嵁涔︾睄锛屽ぇ鏁版嵁楂橀闈㈣瘯棰橈紝娴烽噺涓€绾垮ぇ鍘傞潰缁忊€︽湡寰呮偍鐨勫叧娉?




闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢
绮惧僵鎺ㄨ崘








鐚夸汉鑿?鍙戣捣浜嗕竴涓鑰呰璁?/span> 鐣欒█鍖猴細





鐐逛釜鍦ㄧ湅 浣犳渶濂界湅



以上是关于闆跺熀纭€瀛ginx銆?銆憒 Nginx 甯哥敤鐨勫懡浠ゅ拰閰嶇疆鏂囦欢的主要内容,如果未能解决你的问题,请参考以下文章

[瑙﹀姩绮剧伒]闆跺熀纭€灏忕櫧瀛﹁Е鍔?-8

MySQL 甯哥敤鍛戒护鍜屽熀纭€璇硶

瀛︿範璁″垝

涓€瀛﹀氨浼氱殑git鍛戒护

django甯哥敤妯℃澘璇█