環境說明:
linux centos環境下
操作準備:
已安裝docker
開始:
1.拉取鏡像
docker pull nginx
2.準備一份nginx.conf檔案上傳至/root/nginx/檔案夾下,沒有的話末尾貼出一份配置供使用,注意該檔案編碼格式一定要為UTF-8,否則會讀取失敗,切記,

3.創建nginx容器,組態檔掛載
docker run -d -p 80:80 -v /root/nginx/:/var/log/nginx/ -v /root/nginx/nginx.conf:/etc/nginx/nginx.conf:ro --name nginx nginx
說明:-v 為掛載命令,本示例掛載路徑為/root/nginx/,暴露的埠為80
至此nginx配置完成,可用過IP進行訪問
4.如需更改nginx配置,操作如下
修改docker宿主機的nginx.conf檔案,重啟docker即可生效,重啟指令
docker restart nginx
5.部署完成后,nginx運行和錯誤日志為access.log和error.log檔案

注:如果部署完后未生效,可通過docker logs nginx查看輸出日志定位問題,一般為路徑和檔案編碼問題所致,
示例nginx.conf檔案,注意修改http{}中的負載IP
##代碼塊中的events、http、server、location、upstream等都是塊配置項## ##塊配置項可以嵌套,內層塊直接繼承外層快,例如:server塊里的任意配置都是基于http塊里的已有配置的## ##Nginx worker行程運行的用戶及用戶組 #語法:user username[groupname] 默認:user nobody nobody #user用于設定master行程啟動后,fork出的worker行程運行在那個用戶和用戶組下,當按照"user username;"設定時,用戶組名與用戶名相同, #若用戶在configure命令執行時,使用了引數--user=usergroup 和 --group=groupname,此時nginx.conf將使用引數中指定的用戶和用戶組, #user nobody; ##Nginx worker行程個數:其數量直接影響性能, #每個worker行程都是單執行緒的行程,他們會呼叫各個模塊以實作多種多樣的功能,如果這些模塊不會出現阻塞式的呼叫,那么,有多少CPU內核就應該配置多少個行程,反之,有可能出現阻塞式呼叫,那么,需要配置稍多一些的worker行程, worker_processes 1; ##ssl硬體加速, #用戶可以用OpneSSL提供的命令來查看是否有ssl硬體加速設備:openssl engine -t #ssl_engine device; ##守護行程(daemon),是脫離終端在后臺允許的行程,它脫離終端是為了避免行程執行程序中的資訊在任何終端上顯示,這樣一來,行程也不會被任何終端所產生的資訊所打斷,## ##關閉守護行程的模式,之所以提供這種模式,是為了放便跟蹤除錯nginx,畢竟用gdb除錯行程時最繁瑣的就是如何繼續跟進fork出的子行程了,## ##如果用off關閉了master_proccess方式,就不會fork出worker子行程來處理請求,而是用master行程自身來處理請求 #daemon off; #查看是否以守護行程的方式運行Nginx 默認是on #master_process off; #是否以master/worker方式作業 默認是on ##error日志的設定# #語法: error_log /path/file level; #默認: error_log / log/error.log error; #當path/file 的值為 /dev/null時,這樣就不會輸出任何日志了,這也是關閉error日志的唯一手段; #leve的取值范圍是debug、info、notice、warn、error、crit、alert、emerg從左至右級別依次增大, #當level的級別為error時,error、crit、alert、emerg級別的日志就都會輸出,大于等于該級別會輸出,小于該級別的不會輸出, #如果設定的日志級別是debug,則會輸出所有的日志,這一資料量會很大,需要預先確保/path/file所在的磁盤有足夠的磁盤空間,級別設定到debug,必須在configure時加入 --with-debug配置項, #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; ##pid檔案(master行程ID的pid檔案存放路徑)的路徑 #pid logs/nginx.pid; events { #僅對指定的客戶端輸出debug級別的日志: 語法:debug_connection[IP|CIDR] #這個設定項實際上屬于事件類配置,因此必須放在events{……}中才會生效,它的值可以是IP地址或者是CIRD地址, #debug_connection 10.224.66.14; #或是debug_connection 10.224.57.0/24 #這樣,僅僅以上IP地址的請求才會輸出debug級別的日志,其他請求仍然沿用error_log中配置的日志級別, #注意:在使用debug_connection前,需確保在執行configure時已經加入了--with-debug引數,否則不會生效, worker_connections 1024; } ##核心轉儲(coredump):在Linux系統中,當行程發生錯誤或收到信號而終止時,系統會將行程執行時的記憶體內容(核心映像)寫入一個檔案(core檔案),以作為除錯只用,這就是所謂的核心轉儲(coredump). http { ##嵌入其他組態檔 語法:include /path/file #引數既可以是絕對路徑也可以是相對路徑(相對于Nginx的配置目錄,即nginx.conf所在的目錄) 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; upstream taishan { #weigth引數表示權值,權值越高被分配到的幾率越大 #下面表示9500有3分之2幾率,9501有3分之1幾率 server 47.206.155.885:9500 weight=2; server 47.206.155.885:9501 weight=1; } #gzip on; server { ##listen監聽的埠 #語法:listen address:port [ default(deprecated in 0.8.21) | default_server | [ backlog=num | rcvbuf=size | sndbuf=size | accept_filter=filter | deferred | bind | ssl ] ] #default_server: 如果沒有設定這個引數,那么將會以在nginx.conf中找到的第一個server塊作為默認server塊 listen 80; #主機名稱:其后可以跟多個主機名稱,開始處理一個HTTP請求時,nginx會取出header頭中的Host,與每個server中的server_name進行匹配,以此決定到底由那一個server來處理這個請求,有可能一個Host與多個server塊中的server_name都匹配,這時會根據匹配優先級來選擇實際處理的server塊,server_name與Host的匹配優先級見文末, server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} ##location 語法: location [=|~|~*|^~] /uri/ { ... } # location的使用實體見文末, #注意:location時有順序的,當一個請求有可能匹配多個location時,實際上這個請求會被第一個location處理, location / { proxy_pass http://taishan; } #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://39.96.34.52:8091; #} # 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; #} } # 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; # 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; # location / { # root html; # index index.html index.htm; # } #} }
完事了,
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/6476.html
標籤:Linux
上一篇:linux 創建邏輯卷
下一篇:linux 創建SWAP磁區
