Linux nginx 服務
Nginx服務簡介:
NGINX :是一個自由、開源、高性能、輕量級的HTTP Server 和反向代理服務器,也是一個IMAP/POP3代理服務。nginx因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。
Nginx的出現就是為了解決C10k的問題,C10k:10000個并發連接。
HTTP:基于prefork模型的最大默認并發連接為256個。內部所實現的 I/O處理機制是select而select本身支持的最大并發能力是1024個,雖然基于內核修改完以后能夠突破1024個并發連接,但將會變得極其不穩定。
http協議:
URL:shceme://username:password@host:port/path;params?query#frag
Status Code:狀態代碼
1xx:信息;
2xx:成功類響應碼,200 ok
3xx:重定向類的響應碼,301永久重定向, 302臨時重定向, 304 Not Modified
4xx:客戶端錯誤,403 Forbidden請求被拒絕,404 notFound 請求的資源找不到。
5xx:服務器端錯誤,502 Bod Gateway
http事務:
request:
<method> <URI> <VERSION>
HEADERS
<body>
reponse:
<VERSION> <STATUS> <REASON-PHRASE>
HEADERS
<body>
Method(方法):GET/HEAD/POST, PUT/DELETE, TRACES, OPTIONS
認證方式:
基于ip認證:Require no ip 192.168.3.5 Require all granted
基于用戶認證 basic
httpd MPM(多路處理模塊):
prefork:進程模型,兩級結構,master/worker, 每worker處理一個請求,復用型阻塞I/O模型;
worker:線程模型,三級結構,master/worker/thread,每thread處理一個請求,復用型阻塞I/O模型;
event:事件驅動的線程模型,兩級結構,master/worker,每worker響應多個請求;事件驅動型I/O模型;
I/O模型:
阻塞型、非阻塞型、復用型、事件驅動型、異步
同步/異步:
關注消息通知機制;
消息通知:
同步:等待對方返回消息;
異步:被調用者通過狀態、通知或回調機制通將運行狀態通知給調用者;
阻塞/非阻塞:
關注調用者在等待結果返回之前所處的狀態;
阻塞:blocking,調用結果返回之前,調用者被掛起;
非阻塞:nonblocking,調用結果返回之前,調用者不會被掛起;
一次IO請求,都會由兩階段組成:
第一步:等待數據,即數據從磁盤到內核內存;
第二步:復制數據,即數據內核內存到進程內存;
復用型IO調用:
http prefork select():1024個并發連接。
poll():unix
event-driven:
epoll(Linux):libevent
Kqueue(BSD):
阻塞型I/O模型,
Nginx arch 工作模型:
Nginx特點:
Event-driven 事件驅動
Asynchronous 異步
Non-blocking 非阻塞
Nginx的程序架構:
master/worker
一個master進程:
負載加載配置文件、管理worker進程、平滑升級,無需關停服務,用戶無所感知。
一個或多個worker進程:
處理并響應用戶請求
緩存相關的進程:
cache loader:載入緩存對象
cache manager:管理緩存對象
特性:異步、事件驅動和非阻塞
并發請求處理:通過kevent/epoll/select
文件IO:高級IO sendfile,異步,mmap
sendfile:直接將文件從內核內存發送出去,不需要經過nginx進程進行封裝。
mmap:內存映射,直接將nginx進程所需數據映射到磁盤空間。
nginx高度模塊塊:高度模塊化,但其模塊早期不支持DSO(動態裝卸載)機制;近期版本支持動態裝載和卸載;
模塊分類:
核心模塊:core module
標準模塊:
Standard HTTP modules:標準的http模塊
Optional HTTP modules:可選http模塊
Mail modules :郵件mail相關模塊
Stream modules:流式協議模塊
3rd party modules:第三方模塊;
nginx的功用:
靜態的web資源服務器;
結合FastCGI/uwSGI/SCGI等協議反代動態資源請求;
http/https協議的反向代理;lnamp:Linux+nginx+apache+mariadb+php
imap4/pop3協議的反抽代理;
tcp/udp協議的反代;
nginx的安裝配置:
官方的預制包:
http://nginx.org/packages/centos/7/x86_64/RPMS/
編譯安裝:
~]# yum install pcre-devel openssl-devel zlib-devel
~]# useradd -r nginx
~]# ./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –user=nginx –group=nginx –with-http_ssl_module –with-http_v2_module –with-http_dav_module –with-http_stub_status_module –with-threads –with-file-aio
~]# make && make install
Nginx配置文件:
配置文件的組成部分:
主配置文件:/etc/nginx/nginx.conf
include conf.d/*.conf
fastcgi, uwsgi,scgi等協議相關的配置文件
mime.types:支持的mime類型
主配置文件的配置指令:
directive value [value2 …];
注意:
(1) 指令必須以分號結尾;
(2) 支持使用配置變量;
內建變量:由Nginx模塊引入,可直接引用;
自定義變量:由用戶使用set命令定義;
set variable_name value;
引用變量:$variable_name
Nginx的主配置文件結構:
main block:主配置段,也即全局配置段;
event {
…
}:事件驅動相關的配置;
http {
…
}:http/https 協議相關的配置段;
mail {
…
}
stream {
…
}
http協議相關的配置結構
http {
…
…:各server的公共配置
server {
…
}:每個server用于定義一個虛擬主機;
server {
…
server_name
root
location [OPERATOR] URL {
alias
…
if CONDITION {
…
}
}
}
}
main配置段常見的配置指令:
分類:
正常運行必備的配置
優化性能相關的配置
用于調試及定位問題相關的配置
事件驅動相關的配置
正常運行必備的配置:
1、user
Syntax:user user [group];
Default: user nobody nobody;
Context: main
Defines user and group credentials used by worker processes. If group is omitted, a group whose name equals that of user is used.
2、pid /PATH/TO/PID_FILE;
指定存儲nginx主進程進程號碼的文件路徑;
3、include file | mask;
指明包含進來的其它配置文件片斷;
4、load_module file;
指明要裝載的動態模塊;
性能優化相關的配置:
1、worker_processes number | auto;
worker進程的數量;通常應該為當前主機的cpu的物理核心數或減1;
2、worker_cpu_affinity cpumask …; worker進程綁定cpu
worker_cpu_affinity auto [cpumask];
CPU MASK:
00000001:0號CPU
00000010:1號CPU
… …
示例 :
把4個worker進程固定的綁定在4個CPU上。
或
[root@centos7 nginx]# watch -n1 'ps axo pid,comm,psr | grep nginx'
注意:CPU是從0號開始編號的。
3、worker_priority number;
指定worker進程的nice值,設定worker進程優先級;[-20,20]
4、worker_rlimit_nofile number;
worker進程所能夠打開的文件數量上限;
worker進程所能夠打開的最大文件數量上限應該大于 worker_processes * worker_connections
調試、定位問題:
1、daemon on|off;
是否以守護進程方式運行Nignx;
2、master_process on|off;
是否以master/worker模型運行nginx;默認為on;off通常只為管理員設定;
3、error_log file [level];
事件驅動相關的配置:
events {
…
}
1、worker_connections number;
每個worker進程所能夠打開的最大并發連接數數量;
當前nginx最大并發連接數:worker_processes * worker_connections
2、use method;
指明并發連接請求的處理方法;
默認為:use epoll;
3、accept_mutex on | off;
處理新的連接請求的方法;on意味著由各worker輪流處理新請求,Off意味著每個新請求的到達都會通知所有的worker進程,默認為off,建議使用on。
http協議的相關配置:
http {
… …
server {
…
server_name
root
location [OPERATOR] /uri/ {
…
}
}
server {
…
}
}
1、server { … }
配置一個虛擬主機;
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
2、listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
3、server_name name …;
指明虛擬主機的主機名稱;后可跟多個由空白字符分隔的字符串;
支持*通配任意長度的任意字符;server_name *.magedu.com
支持~起始的字符做正則表達式模式匹配;server_name ~^www\d+\.magedu\.com$
匹配機制:
(1) 首先是字符串精確匹配;
(2) 左側*通配符;
(3) 右側*通配符;
(4) 正則表達式;
注意:通配符比正則表達式要優先匹配。
示例:
1)在nginx主配置文件中創建基于端口的虛擬機:
在http配置段中如下:
2)在nginx主配置文件中創建基于ip的虛擬機:
3)在nginx主配置文件中創建基于主機名的虛擬機:
4)在nginx服務本機設置防火墻,只允許本地網絡訪問80服務:
4、tcp_nodelay on | off;(nodelay不延遲)
在keepalived模式下的連接是否啟用TCP_NODELAY選項;
5、sendfile on | off;
是否啟用sendfile功能;
定義路徑相關的配置:
6、root path;
設置web資源路徑映射;用于指明用戶請求的url所對應的本地文件系統上的文檔所在目錄路徑;可用的位置:http, server, location, if in location;
7、location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
在一個server中location配置段可存在多個,用于實現從uri到文件系統的路徑映射;ngnix會根據用戶請求的URI來檢查定義的所有location,并找出一個最佳匹配,而后應用其配置;
=:對URI做精確匹配;例如, http://www.magedu.com/,
~:對URI做正則表達式模式匹配,區分字符大小寫;
~*:對URI做正則表達式模式匹配,不區分字符大小寫;
^~:對URI的左半部分做匹配檢查,不區分字符大小寫;
不帶符號:匹配起始于此uri的所有的url;
匹配優先級:=, ^~, ~/~*,不帶符號;
8、alias path;
定義路徑別名,文檔映射的另一種機制;僅能用于location上下文;
注意:location中使用root指令和alias指令的意義不同;
(a) root,給定的路徑對應于location中的/uri/左側的/;
(b) alias,給定的路徑對應于location中的/uri/右側的/;
9、index file …;
默認資源;http, server, location;
10、error_page code … [=[response]] uri;
Defines the URI that will be shown for the specified errors.
11、try_files file … uri;
定義客戶端請求的相關配置
12、keepalive_timeout timeout [header_timeout];
設定保持連接的超時時長,0表示禁止長連接;默認為75s;
13、keepalive_requests number;
在一次長連接上所允許請求的資源的最大數量,默認為100;
14、keepalive_disable none | browser …;
對哪種瀏覽器禁用長連接;
15、send_timeout time;
向客戶端發送響應報文的超時時長,此處,是指兩次寫操作之間的間隔時長;
16、client_body_buffer_size size;
用于接收客戶端請求報文的body部分的緩沖區大小;默認為16k;超出此大小時,其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置;
17、client_body_temp_path path [level1 [level2 [level3]]];
設定用于存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量;
16進制的數字;
client_body_temp_path path /var/tmp/client_body 1 2 2
對客戶端進行限制的相關配置:
18、limit_rate rate;
限制響應給客戶端的傳輸速率,單位是bytes/second,0表示無限制;
19、limit_except method … { … }
限制對指定的請求方法之外的其它方法的使用客戶端;
示例:限制除get之外的所有請求方法為拒絕。
limit_except GET {
allow 192.168.1.0/32;
deny all;
}
文件操作優化的配置:
20、aio on | off | threads[=pool];
是否啟用aio功能;
21、directio size | off;
在Linux主機啟用O_DIRECT標記,此處意味文件大于等于給定的大小時使用,例如directio 4m;
22、open_file_cache off;
open_file_cache max=N [inactive=time];
nginx可以緩存以下三種信息:
(1) 文件的描述符、文件大小和最近一次的修改時間;
(2) 打開的目錄結構;
(3) 沒有找到的或者沒有權限訪問的文件的相關信息;
max=N:可緩存的緩存項上限;達到上限后會使用LRU算法實現緩存管理;
inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次數少于open_file_cache_min_users指令所指定的次數的緩存項即為非活動項;
23、open_file_cache_valid time;
緩存項有效性的檢查頻率;默認為60s;
24、open_file_cache_min_uses number;
在open_file_cache指令的inactive參數指定的時長內,至少應該被命中多少次方可被歸類為活動項;
25、open_file_cache_errors on | off;
是否緩存查找時發生錯誤的文件一類的信息;
ngx_http_access_module模塊:
實現基于ip的訪問控制功能
26、allow address | CIDR | unix: | all;
27、deny address | CIDR | unix: | all;
http, server, location, limit_except
ngx_http_auth_basic_module模塊
實現基于用戶的訪問控制,使用basic機制進行用戶認證;
28、auth_basic string | off;
29、auth_basic_user_file file;
location /admin/ {
alias /webapps/app1/data/;
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.ngxpasswd;
}
注意:htpasswd命令由httpd-tools所提供;
示例:
關閉basic認證功能:
ngx_http_stub_status_module模塊
用于輸出nginx的基本狀態信息;
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
Active connections: 活動狀態的連接數;
accepts:已經接受的客戶端請求的總數;
handled:已經處理完成的客戶端請求的總數;
requests:客戶端發來的總的請求數;
Reading:處于讀取客戶端請求報文首部的連接的連接數;
Writing:處于向客戶端發送響應報文過程中的連接數;
Waiting:處于等待客戶端發出請求的空閑連接數;
30、stub_status;
配置示例:
location /basic_status {
stub_status;
}
ngx_http_log_module模塊
he ngx_http_log_module module writes request logs in the specified format.
31、log_format name string …;
string可以使用nginx核心模塊及其它模塊內嵌的變量;
課外作業:為nginx定義使用類似于httpd的combined格式的訪問日志;
32、access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
訪問日志文件路徑,格式及相關的緩沖的配置;
buffer=size
flush=time
33、open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
緩存各日志文件相關的元數據信息;
max:緩存的最大文件描述符數量;
min_users:在inactive指定的時長內訪問大于等于此值方可被當作活動項;
inactive:非活動時長;
valid:驗正緩存中各緩存項是否為活動項的時間間隔;
ngx_http_rewrite_module模塊:
The ngx_http_rewrite_module module is used to change request URI using PCRE regular expressions, return redirects, and conditionally select configurations.
將用戶請求的URI基于regex所描述的模式進行檢查,而后完成替換;
1、rewrite regex replacement [flag]
將用戶請求的URI基于regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI;
注意:如果在同一級配置塊中存在多個rewrite規則,那么會自上而下逐個檢查;被某條件規則替換完成后,會重新一輪的替換檢查,因此,隱含有循環機制;[flag]所表示的標志位用于控制此循環機制;
如果replacement是以http://或https://開頭,則替換結果會直接以重定向返回給客戶端;
301:永久重定向;
[flag]:
last:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重啟新一輪循環;
break:重寫完成后停止對當前URI在當前location中后續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它配置;結束循環;
redirect:重寫完成后以臨時重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;不能以http://或https://開頭;
permanent:重寫完成后以永久重定向方式直接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;
示例: break終止循環;
2、return
return code [text];
return code URL;
return URL;
Stops processing and returns the specified code to a client.
3、 rewrite_log on | off;
是否開啟重寫日志;
4、 if (condition) { … }
引入一個新的配置上下文 ;條件滿足時,執行配置塊中的配置指令;server, location;
condition(條件):
比較操作符:
==
!=
~:模式匹配,區分字符大小寫;
~*:模式匹配,不區分字符大小寫;
!~:模式不匹配,區分字符大小寫;
!~*:模式不匹配,不區分字符大小寫;
文件及目錄存在性判斷:
-e, !-e
-f, !-f
-d, !-d
-x, !-x
5、set $variable value;
用戶自定義變量 ;
ngx_http_gzip_module:
The ngx_http_gzip_module module is a filter that compresses responses using the “gzip” method. This often helps to reduce the size of transmitted data by half or even more.
1、gzip on | off;
Enables or disables gzipping of responses.
2、gzip_comp_level level;
Sets a gzip compression level of a response. Acceptable values are in the range from 1 to 9.
3、 gzip_disable regex …;
Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.
4、 gzip_min_length length;
啟用壓縮功能的響應報文大小閾值;
5、gzip_buffers number size;
支持實現壓縮功能時為其配置的緩沖區數量及每個緩存區的大小;
6、gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any …;
nginx作為代理服務器接收到從被代理服務器發送的響應報文后,在何種條件下啟用壓縮功能的;
off:對代理的請求不啟用
no-cache, no-store,private:表示從被代理服務器收到的響應報文首部的Cache-Control的值為此三者中任何一個,則啟用壓縮功能;
7、gzip_types mime-type …;
壓縮過濾器,僅對此處設定的MIME類型的內容啟用壓縮功能;
ngx_http_fastcgi_module模塊:
The ngx_http_fastcgi_module module allows passing requests to a FastCGI server.
1、fastcgi_pass address;
address為fastcgi server的地址; location, if in location;
2、fastcgi_index name;
fastcgi默認的主頁資源;
3、fastcgi_param parameter value [if_not_empty];
從nginx下傳遞一個參數給php-fpm
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script _name;
Sets a parameter that should be passed to the FastCGI server. The value can contain text,
variables, and their combination.
配置示例1:
前提:配置好fpm server和mariadb-server服務;
location ~* \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
配置示例2:通過/pm_status和/ping來獲取fpm server狀態信息;
location ~* ^/(pm_status|ping)/?$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
4、fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
定義fastcgi的緩存;緩存位置為磁盤上的文件系統,由path所指定路徑來定義;
levels=levels:緩存目錄的層級數量,以及每一級的目錄數量;levels=ONE:TWO:THREE
leves=1:2:2
keys_zone=name:size
k/v映射的內存空間的名稱及大小
inactive=time
非活動時長
max_size=size
磁盤上用于緩存數據的緩存空間上限
5、fastcgi_cache zone | off;
調用指定的緩存空間來緩存數據;http, server, location
6、fastcgi_cache_key string;
定義用作緩存項的key的字符串;
7、fastcgi_cache_methods GET | HEAD | POST …;
為哪些請求方法使用緩存;
8、fastcgi_cache_min_uses number;
緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項;
9、fastcgi_cache_valid [code …] time;
不同的響應碼各自的緩存時長;
示例:
http {
…
fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
…
server {
…
location ~* \.php$ {
…
fastcgi_cache fcgi;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
…
}
…
}
…
}
10、fastcgi_keep_conn on | off;
開啟fastcgi保持連接功能;
ngx_http_ssl_module模塊:
1、 ssl on | off;
Enables the HTTPS protocol for the given virtual server.
2、ssl_certificate file;
當前虛擬主機使用PEM格式的證書文件;
3、ssl_certificate_key file;
當前虛擬主機上與其證書匹配的私鑰文件;
4、ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
支持ssl協議版本,默認為后三個;
5、ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
builtin[:size]:使用OpenSSL內建的緩存,此緩存為每worker進程私有;
[shared:name:size]:在各worker之間使用一個共享的緩存;
6、ssl_session_timeout time;
客戶端一側的連接可以復用ssl session cache中緩存 的ssl參數的有效時長;
配置示例:
server {
listen 443 ssl;
server_name www.magedu.com;
root /vhosts/ssl/htdocs;
ssl on;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:sslcache:20m;
}
1)編輯nginx配置文件:
(1)配置/etc/nginx/conf.d/ssl.conf
(2)配置/etc/nginx/conf.d/default.conf使其不管訪問的uri是什么都將它重定向至https://192.168.3.5
ngx_http_referer_module模塊: referer(引用)
防止盜鏈
The ngx_http_referer_module module is used to block access to a site for requests with invalid values in the “Referer” header field.
1、valid_referers none | blocked | server_names | string …;
定義referer首部的合法可用值;
none:請求報文首部沒有referer首部;
blocked:請求報文的referer首部沒有值;
server_names:參數,其可以有值作為主機名或主機名模式;
arbitrary_string:直接字符串,但可使用*作通配符;
regular expression:被指定的正則表達式模式匹配到的字符串;要使用~打頭,例如 ~.*\.magedu\.com;
配置示例:
valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;
if($invalid_referer) {
return 403;
}
測試防盜鏈網頁:
配置fastcgi緩存
定義fsatcgi的緩存,緩存位置為磁盤上的文件系統,由fastcgi_cache_path指定的路徑來定義及 levels、keys_zone、inactive:
1)創建緩存目錄并更改屬主屬組為nginx:
2)定義fastcgi緩存路徑,fastcgi_cache_path只能定義在http配置段中:
3)要調用fastcgi緩存,只能在server或location中調用:
4)查看生成的緩存目錄:
實驗測試:
編譯安裝nginx服務:
1)安裝編譯時的開發環境包組:
yum -y groupinstall "Development Tools" "Server Platform Development"
2)安裝所需軟件包依賴關系:支持正則表達式、ssl、和數據傳輸時壓縮傳送
yum -y install pcre-devel openssl-devel zlib-devel
3)創建添加一個系統用戶:
[root@centos7 ~]# useradd -r nginx
4)解壓nginx-1.10.0.tar.gz:
~]# tar xf nginx-1.10.0.tar.gz
~]# cd nginx-1.10.0/
5)配置nginx所需的編譯路徑及模塊:
[root@centos7 nginx-1.10.0]# ./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –user=nginx –group=nginx –with-http_ssl_module –with-http_v2_module –with-http_dav_module –with-http_stub_status_module –with-threads –with-file-aio
6)編譯和安裝nginx服務:
[root@centos7 nginx-1.10.0]# make -j 4 && make install
7)導入/usr/local/nginx/sbin到PATH環境變量中:
[root@centos7 nginx-1.10.0]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@centos7 nginx-1.10.0]# . /etc/profile.d/nginx.sh
8)啟動nginx:如果你已經啟動httpd服務,nginx是無法啟動的,會報錯。
直接寫nginx回車即可;
[root@centos7 nginx]# nginx
實驗測試:
yum安裝nginx,安裝新版本的nginx-1.10.0-1.el7.ngx.x86_64.rpm,需去nginx官方站點下載此rpm包。
1)yum 安裝nginx:
~]# yum -y install ./nginx-1.10.0-1.el7.ngx.x86_64.rpm
2)啟動nginx服務:
systemctl start nginx.service
3)安裝mariadb數據庫和php-fpm
yum -y install mariadb-server php-fpm php-mysql php-mcrypt php-gd php-mstring
4)為php-fpm創建session會話目錄,并設置nginx為session目錄的屬主和屬組。
mkdir /var/lib/php/session
chown -R nginx:nginx /var/lib/php/session
5)編輯/etc/php-fpm.d/www.conf目錄設置php-fpm監聽地址及用戶和組為nginx。
啟動php-fpm:
systemctl start php-fpm.service
6)配置nginx配置文檔及fastcgi狀態測試:
[root@centos7 html]# vim /etc/nginx/conf.d/default.conf
7)編輯一個php測試頁:
vim /usr/share/nginx/html/phpinfo.php
8)測試:
9)檢測fastCGI狀態及ping連通性:
試驗測試,為nginx創建ssl連接:
實驗環境CA為centos6 www.magedu.com服務器為centos7:
1)創建/vhosts/ssl目錄和index.html測試頁:
mkdir -pv /vhosts/ssl/
2)編輯nginx配置文件:
(1)配置/etc/nginx/conf.d/ssl.conf
(2)配置/etc/nginx/conf.d/default.conf使其不管訪問的uri是什么都將它重定向至https://192.168.3.5
3)創建私有CA:
(1)生成私有CA的私鑰文件:
(2)私有CA自簽證書:
(3)創建CA數據庫文件和CA數據庫索引編號文件:
(4)查看CA自簽證書信息:
5)在www.magedu.com服務器端生成私鑰文件:
6)生成證書申請文件:
7)使用scp命令把證書申請文件傳遞給CA:
8)給www.magedu.com頒發證書。
9)使用scp命令將www.magedu.com證書copy到www.magedu.com服務器的指定目錄下:
10)查看wwwmagedu.com服務器/etc/nginx/ssl目錄下的文件:
11)查看www.magedu.com的證書:
使用https測試訪問:
原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/59604