linux下nginx安裝配置
前言
環境:centos 7.4,nginx 1.12.2
場景:現有一臺外網輕量級服務器。dns會將*.ijianghe.cn這個根域統統解析成該服務器的IP。需求如下
-
- 網站主頁業務
- 對應目錄:/data/wwwsite/www/
當訪問www.ijianghe.cn時加載/data/wwwsite/www/index.html
當訪問www.ijianghe.cn/wwwapi/(apiname)時訪問127.0.0.1:9527的本地主頁nodejs服務
-
mob.ijianghe.cn
- 網站移動業務
- 對應目錄:/data/wwwsite/mob/
當訪問mob.ijianghe.cn時加載/data/wwwsite/mob/index.html
當訪問mob.ijianghe.cn/mobapi/(apiname)時訪問127.0.0.1:9528的本地移動端nodejs服務
nginx安裝部分:
- 安裝gc++
yum install gcc-c++
- 安裝PCRE pcre-devel
yum install -y pcre pcre-devel
- 安裝zlib
yum install -y zlib zlib-devel
- 安裝OpenSSL
yum install -y openssl openssl-devel
- 更新下rpm依賴庫
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
- 安裝nginx
yum -y install nginx
- 啟動Nginx
service nginx start curl http://127.0.0.1 #如果出現nginx字樣說明nginx啟動成功
nginx常用命令
-
查看nginx版本號
nginx -v
-
查看nginx配置文件路徑
nginx -t
-
停止nginx服務
nginx -c stop
-
啟動nginx服務
nginx #或者 service nginx start #或者 start nginx
-
開機自啟動nginx
vi /etc/rc.local #增加一行 /usr/sbin/nginx(不同版本在不同路徑下 可用which nginx查看nginx指令在哪) chmod 755 rc.local #設置權限755
通過配置nginx來實現前言中的需求
-
準備日志目錄和項目根目錄
mkdir /etc/nginx/nginxhost #在nginx主配置文件同級目錄新建nginxhost文件夾來放置nginx虛擬主機配置 mkdir /data/logs #在開發目錄下新建logs文件用來防止nginx的所有日志文件
-
1.編輯nginx主配置文件
nginx -t #查看主配置文件位置 vi /etc/nginx/nginx.conf #編輯nginx主配置文件 user nginx; #使用者 worker_processes 2; #進程數一遍為cpu核數的1-2倍 #worker_cpu_affinity 01 10; #01表示啟用第一個CPU內核,10表示啟用第二個CPU內核 events { use epoll; 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 /data/logs/access.log main; #輸入日志的名字和路徑 sendfile on; #tcp_nopush on; keepalive_timeout 65; gzip on; #開啟gzip壓縮http傳輸的內容 include /etc/nginx/nginxhost/*.conf; #使用通配符引入所有虛擬主機配置文件 }
-
2.配置nginx虛擬主機配置
touch /etc/nginx/nginxhost/www.ijianghe.cn.conf; #新建www.ijianghe.cn的配置文件 vi /etc/nginx/nginxhost/www.ijianghe.cn.conf; #編輯www.ijianghe.cn配置文件 server{ #入口端口 listen 80; #入口服務名可以多寫空格分開 server_name www.ijianghe.cn www.test.ijianghe.cn; #當入口為www.ijianghe.cn 80端口時將根路徑設置到/data/wwwsite/www路徑 root /data/wwwsite/www; #日志文件輸出位置 access_log /data/logs/www.ijianghe.cn.log main; #off時 url定向為原url+目錄名+/ ; #on時 url定向為server_name中第一個域名+目錄名+/; server_name_in_redirect off; #目錄為/時加載root+index路徑下的靜態資源(也就是/data/wwwsite/www/index.html); location / { index index.html; } #= 嚴格匹配。如果這個查詢匹配,那么將停止搜索并立即處理此請求。 #~ 為區分大小寫匹配(可用正則表達式) #!~為區分大小寫不匹配 #~* 為不區分大小寫匹配(可用正則表達式) #!~*為不區分大小寫不匹配 #^~ 如果把這個前綴用于一個常規字符串,那么告訴nginx 如果路徑匹配那么不測試正則表達式。 #如果匹配這個正則將代理權交給本地9527端口的本地服務 location ~ ^/wwwapi/(.*)$ { proxy_pass http://127.0.0.1:9527/wwwapi/$1; } #定義錯誤提示頁面 error_page 404 /404.html; #禁止訪問 .htxxx 文件 location ~ /.ht { deny all; } }
- 2.配置mob.ijianghe.cn.conf(解釋如上一個配置不贅述)
touch /etc/nginx/nginxhost/mob.ijianghe.cn.conf; vi /etc/nginx/nginxhost/mob.ijianghe.cn.conf; server{ listen 80; server_name mob.ijianghe.cn mob.test.ijianghe.cn; root /data/wwwsite/mob; access_log /data/logs/mob.ijianghe.cn.log main; server_name_in_redirect off; location / { index index.html; } location ~ ^/mobapi/(.*)$ { proxy_pass http://127.0.0.1:9528/mobapi/$1; } error_page 404 /404.html; location ~ /.ht { deny all; } }
其他
- 主配置詳解
#運行用戶 user nobody; #啟動進程,通常設置成和cpu的數量相等 worker_processes 2; #01表示啟用第一個CPU內核,10表示啟用第二個CPU內核 #worker_cpu_affinity 01 10; #全局錯誤日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; #工作模式及連接數上限 events { #epoll是多路復用IO(I/O Multiplexing)中的一種方式, #僅用于linux2.6以上內核,可以大大提高nginx的性能 use epoll; #單個后臺worker process進程的最大并發鏈接數 worker_connections 1024; # 并發總數是 worker_processes 和 worker_connections 的乘積 # 即 max_clients = worker_processes * worker_connections # 在設置了反向代理的情況下,max_clients = worker_processes * worker_connections / 4 為什么 # 為什么上面反向代理要除以4,應該說是一個經驗值 # 根據以上條件,正常情況下的Nginx Server可以應付的最大連接數為:4 * 8000 = 32000 # worker_connections 值的設置跟物理內存大小有關 # 因為并發受IO約束,max_clients的值須小于系統可以打開的最大文件數 # 而系統可以打開的最大文件數和內存大小成正比,一般1GB內存的機器上可以打開的文件數大約是10萬左右 # 我們來看看360M內存的VPS可以打開的文件句柄數是多少: # $ cat /proc/sys/fs/file-max # 輸出 34336 # 32000 < 34336,即并發連接總數小于系統可以打開的文件句柄總數,這樣就在操作系統可以承受的范圍之內 # 所以,worker_connections 的值需根據 worker_processes 進程數目和系統可以打開的最大文件總數進行適當地進行設置 # 使得并發總數小于操作系統可以打開的最大文件數目 # 其實質也就是根據主機的物理CPU和內存進行配置 # 當然,理論上的并發總數可能會和實際有所偏差,因為主機還有其他的工作進程需要消耗系統資源。 # ulimit -SHn 65535 } http { #設定mime類型,類型由mime.type文件定義 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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件, #對于普通應用,必須設為 on, #如果用來進行下載等應用磁盤IO重負載應用,可設置為 off, #以平衡磁盤與網絡I/O處理速度,降低系統的uptime. sendfile on; #tcp_nopush on; #連接超時時間 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #開啟gzip壓縮 gzip on; gzip_disable "MSIE [1-6]."; #設定請求緩沖 client_header_buffer_size 128k; large_client_header_buffers 4 128k; #設定虛擬主機配置 server { #偵聽80端口 listen 80; #定義使用 www.nginx.cn訪問 server_name www.nginx.cn; #定義服務器的默認網站根目錄位置 root html; #設定本虛擬主機的訪問日志 access_log logs/nginx.access.log main; #默認請求 location / { #定義首頁索引文件的名稱 index index.php index.html index.htm; } # 定義錯誤提示頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { } #靜態文件,nginx自己處理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { #過期30天,靜態文件不怎么更新,過期可以設大一點, #如果頻繁更新,則可以設置得小一點。 expires 30d; } #禁止訪問 .htxxx 文件 location ~ /.ht { deny all; } } }
- 泛域名解析
server { listen 80; server_name ~^(.+)?\.domain\.com$; #通過正則匹配server_name access_log logs/host.access.log; set $sub $1; #設置sub變量 set $root /www/$1.domain.com; #設置web目錄 set $index index.html; #設置默認的index頁面 if (!-d $root){ set $root /www/domain.com; #如果沒匹配域名 使用默認根地址 } root $root; location / { #通過正則的域名判斷下一步操作 if ($sub = a){ set $index login.html; } if ($sub = b){ set $index login.html; } if ($sub = c){ set $index proxy.php; } index $index; } # 測試服務器不緩存圖片和js.css文件 # location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { # expires 10m; # } # location ~ .*\.(js|css)?$ { # expires 10m; # } location ~ /\.ht { deny all; } }