天天看點

Nginx安裝、預設虛拟主機、使用者認證、nginx中PHP解析12.6 Nginx安裝12.7 Nginx預設虛拟主機12.8 Nginx使用者認證12.9 Nginx域名重定向擴充:Nginx配置檔案詳解

12.6 Nginx安裝

準備工作

安裝包

[root@centos-01 ~]# cd /usr/local/src/

下載下傳安裝包:

[root@centos-01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz

解壓:

[root@centos-01 src]# tar zxvf nginx-1.12.1.tar.gz

安裝

環境配置

[root@centos-01 src]# cd nginx-1.12.1/

[root@centos-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx

#如果需要支援某子產品,可以在此添加,如HTTPS、SSL等

編譯&安裝

[root@centos-01 nginx-1.12.1]# make && make install

[root@centos-01 nginx-1.12.1]# echo $?

[root@centos-01 nginx-1.12.1]# cd /usr/local/nginx/

[root@centos-01 nginx]# ls

conf html logs sbin

配置

添加&啟動服務

建立啟動腳本:

[root@centos-01 nginx]# vim /etc/init.d/nginx

把下面整段複制進去:

#!/bin/bash

#chkconfig: - 30 21

#description: http service.

#Source Function Library

. /etc/init.d/functions

#Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"

NGINX_CONF="/usr/local/nginx/conf/nginx.conf"

NGINX_PID="/usr/local/nginx/logs/nginx.pid"

RETVAL=0

prog="Nginx"

start()

{

echo -n $"Starting $prog: "

mkdir -p /dev/shm/nginx_temp

daemon $NGINX_SBIN -c $NGINX_CONF

RETVAL=$?

echo

return $RETVAL

}

stop()

echo -n $"Stopping $prog: "

killproc -p $NGINX_PID $NGINX_SBIN -TERM

rm -rf /dev/shm/nginx_temp

reload()

echo -n $"Reloading $prog: "

killproc -p $NGINX_PID $NGINX_SBIN -HUP

restart()

stop

start

configtest()

$NGINX_SBIN -c $NGINX_CONF -t

return 0

case "$1" in

start)

;;

stop)

reload)

reload

restart)

restart

configtest)

configtest

*)

echo $"Usage: $0 {start|stop|reload|restart|configtest}"

RETVAL=1

esac

exit $RETVAL

檢查腳本文法:

[root@centos-01 nginx]# /usr/local/nginx/sbin/nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

更改權限:

[root@centos-01 nginx]# chmod 755 /etc/init.d/nginx

添加到系統服務:

[root@centos-01 nginx]# chkconfig --add nginx

[root@centos-01 nginx]# chkconfig nginx on

更改配置檔案

[root@centos-01 nginx]# cd /usr/local/nginx/conf/

注釋掉Nginx自帶腳本,建立自己的腳本:

[root@centos-01 conf]# mv nginx.conf nginx.conf.bak

[root@centos-01 conf]# vim nginx.conf

user nobody nobody;

#定義啟動Nginx的使用者

worker_processes 2;

#定義子程序數目

error_log /usr/local/nginx/logs/nginx_error.log crit;

pid /usr/local/nginx/logs/nginx.pid;

worker_rlimit_nofile 51200;

#指定Nginx最多可打開的檔案數目

events

use epoll;

worker_connections 6000;

#程序最大連接配接數

http

include mime.types;

default_type application/octet-stream;

server_names_hash_bucket_size 3526;

server_names_hash_max_size 4096;

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'

' $host "$request_uri" $status'

' "$http_referer" "$http_user_agent"';

sendfile on;

tcp_nopush on;

keepalive_timeout 30;

client_header_timeout 3m;

client_body_timeout 3m;

send_timeout 3m;

connection_pool_size 256;

client_header_buffer_size 1k;

large_client_header_buffers 8 4k;

request_pool_size 4k;

output_buffers 4 32k;

postpone_output 1460;

client_max_body_size 10m;

client_body_buffer_size 256k;

client_body_temp_path /usr/local/nginx/client_body_temp;

proxy_temp_path /usr/local/nginx/proxy_temp;

fastcgi_temp_path /usr/local/nginx/fastcgi_temp;

fastcgi_intercept_errors on;

tcp_nodelay on;

gzip on;

gzip_min_length 1k;

gzip_buffers 4 8k;

gzip_comp_level 5;

gzip_http_version 1.1;

gzip_types text/plain application/x-javascript text/css text/htm

application/xml;

server

#虛拟主機

listen 80;

server_name localhost;

index index.html index.htm index.php;

root /usr/local/nginx/html;

location ~ .php$

#配置PHP解析

include fastcgi_params;

fastcgi_pass unix:/tmp/php-fcgi.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;

}

檢測文法:

[root@centos-01 conf]# /usr/local/nginx/sbin/nginx -t

啟動Nginx服務:

[root@centos-01 conf]# /etc/init.d/nginx start

Starting nginx (via systemctl): [ 确定 ]

至此,Nginx基礎配置完成!

檢測

[root@centos-01 conf]# curl localhost

<!DOCTYPE html>

<html>

<head>

<title>Welcome to nginx!</title>

<style>

body {

width: 35em;

margin: 0 auto;

font-family: Tahoma, Verdana, Arial, sans-serif;

</style>

</head>

<body>

Welcome to nginx!

<p>If you see this page, the nginx web server is successfully installed and

working. Further configuration is required.</p>

<p>For online documentation and support please refer to

<a rel="nofollow" href="http://nginx.org/">nginx.org</a>.<br/>;

Commercial support is available at

<a rel="nofollow" href="http://nginx.com/">nginx.com</a>.</p>;

<p><em>Thank you for using nginx.</em></p>

</body>

</html>

使用浏覽器檢測:

先将虛拟主機IP添加到本地hosts,然後通路:

檢測PHP解析

[root@centos-01 conf]# vim /usr/local/nginx/html/1.php

<?php

echo "welcom to adai-nginx text.";

?>

[root@centos-01 conf]# curl localhost/1.php

welcom to akai-nginx text.

常見的502問題解決

對于LNMP來說,最常見的就是502問題,LNMP環境搭建完成後,一通路網站直接提示“502 Bad Gateway”。主要原因大緻分為兩種:

(1)配置錯誤

在Nginx配置中有這麼一段:

如果把fastcgi_pass(這是用來通信的)後面指定的路徑配置錯了,那麼就會出現502錯誤,因為Nginx找不到php-fpm了,fastcgi _pass後面可以跟socket也可以跟IP:port,預設監聽位址為:127.0.0.1:9000。

注意: 這裡用兩種形式都可以,但是兩個配置檔案(Nginx和php-fpm)中的形式一定要統一,不然絕對502;如果用套接字形式的話,socket檔案的路徑一定要對,不然也還是502。

(2)資源耗盡

LNMP架構處理PHP時,是Nginx直接調取後端的php-fpm服務,如果Nginx的請求量偏高,而我們又沒給php-fpm配置足夠的子程序數,那麼總有php-fpm資源耗盡的時候,一旦耗盡Nginx找不到php-fpm,此時也會導緻502錯誤出現。解決辦法就是調整php-fpm.conf中的pm.max_children數值,使其增加。但也不能無限制增加,因為伺服器的資源有限。4G記憶體機器如果隻跑php-fpm和Nginx,不跑MySQL服務,pm.max _children可以設定為150,盡量不要超過該數值,8G記憶體設定為300,以此類推。

(3)listen.mode

在php-fpm配置檔案中有參數listen.mode,該參數時指定php-fpm所監聽的socket檔案listen = /tmp/php-fcgi.sock的權限,如果在此不指定權限,預設權限為440(隻允許root使用者及root組讀取),之後在Nginx中監聽該檔案時就會提示502錯誤,解決辦法就是給予socket檔案讀寫權限666。

如果遇到其它的較為少見的錯誤,我們可以修改nginx的錯誤日志(/usr/local/nginx/logs/nginx_error.log)的級别,在配置檔案/usr/local/nginx/conf/nginx.conf中将crit改為debug,使其記錄最多的日志内容,這樣友善我們排查錯誤,但是配置更改完成後要記得将級别改回crit,避免日志檔案占用太多磁盤空間。

12.7 Nginx預設虛拟主機

編輯Nginx配置檔案,删除原有server内容,添加如下内容:

建立虛拟主機

添加虛拟主機目錄

[root@centos-01 ~]# cd /usr/local/nginx/conf

[root@centos-01 conf]# vim /usr/local/nginx/conf/nginx.conf

……

include vhost/*.conf;

#建立一個虛拟主機配置檔案子目錄(相當于增加子虛拟主機)

建立配置檔案中的目錄檔案:

[root@centos-01 conf]# mkdir vhost

注: “nginx.conf”檔案中支援“include”文法。

增加一台虛拟主機:

[root@centos-01 conf]# cd vhost

[root@centos-01 vhost]# vim aaa.com.conf

listen 80 default_server;

#有'default_server'标記的就是預設虛拟主機

server_name aaa.com;

root /data/wwwroot/default;

建立配置檔案中指定的root目錄:

[root@centos-01 vhost]# mkdir -p /data/wwwroot/default

為虛拟主機添加内容

進入目錄,添加索引頁:

[root@centos-01 vhost]# cd /data/wwwroot/default

[root@centos-01 default]# vim index.html

This is the default directory.

[root@centos-01 default]# /usr/local/nginx/sbin/nginx -t

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

重新開機或重新加載(二選一):

[root@centos-01 default]# /usr/local/nginx/sbin/nginx -s reload

[root@centos-01 default]# /usr/local/nginx/sbin/nginx restart

[root@centos-01 default]# curl localhost

Nginx安裝、預設虛拟主機、使用者認證、nginx中PHP解析12.6 Nginx安裝12.7 Nginx預設虛拟主機12.8 Nginx使用者認證12.9 Nginx域名重定向擴充:Nginx配置檔案詳解
即:添加一台虛拟主機,所謂預設虛拟主機就是/usr/local/nginx/conf/vhost目錄下虛拟主機配置檔案中有“default_server”标記的虛拟主機。

12.8 Nginx使用者認證

建立一台虛拟主機:

在vhost目錄下操作:

[root@centos-01 vhost]# vim test.com.conf

server_name test.com;

root /data/wwwroot/test.com;

location /

#指定設定使用者認證的目錄

auth_basic "Auth";

#指定使用者名

auth_basic_user_file /usr/local/nginx/conf/htpasswd;

#指定使用者的密碼檔案

說明: 上述“location”中的内容即為設定使用者認證。在此是為整個站點設定的使用者認證,如果隻是為某個目錄設定使用者認證,在location所在行進行編輯就好,如:location /admin 目錄。也可以對某種請求(即對一個普通檔案)設定使用者認證,如location ~ admin.php()使用 ~ 進行比對)。

建立密碼檔案

在此需要使用Apache的/usr/local/apache/bin/htpasswd指令,如果機器中已經有Apache,可以直接使用,如果沒有,需要使用yum安裝httpd指令:

[root@centos-01 vhost]# yum install -y httpd

建立密碼檔案:

[root@centos-01 vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd akai

New password:

Re-type new password:

Adding password for user adai

即,建立密碼檔案htpasswd,指定使用者為adai。‘-c’=create,建立該密碼檔案,如果是第二次添加使用者,不用加該選項,所添加的使用者名和密碼會儲存到該檔案下。

重載:

[root@centos-01 vhost]# /usr/local/nginx/sbin/nginx -t

[root@centos-01 vhost]# /usr/local/nginx/sbin/nginx -s reload

說明: 使用reload而不使用restart的好處是能避免因配置檔案中存在錯誤而無法正常啟動!reload不會破壞原有運作環境。

添加指定目錄

添加虛拟主機配置檔案指定的根目錄:

[root@centos-01 vhost]# mkdir /data/wwwroot/test.com

添加索引頁:

[root@centos-01 vhost]# echo "This is test.com" >/data/wwwroot/test.com/index.html

[root@centos-01 vhost]# curl -uakai:123456 -x127.0.0.1:80 test.com

This is test.com

注: 如果不指定使用者名和密碼,會報錯401(需要使用者認證);如果為建立虛拟主機根目錄會報錯404(找不到指定目錄);如果指定目錄中沒有添加索引頁(.html或.php檔案)會報錯404(檔案存在錯誤)。

Nginx安裝、預設虛拟主機、使用者認證、nginx中PHP解析12.6 Nginx安裝12.7 Nginx預設虛拟主機12.8 Nginx使用者認證12.9 Nginx域名重定向擴充:Nginx配置檔案詳解

配置虛拟主機PHP解析:

編輯配置檔案,添加如下location内容:

[root@centos-01 vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf

location ~ \.php$
    #配置PHP解析
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/wwwroot/test.com$fastcgi_script_name;
    }           

注意: “fastcgi_param SCRIPT_FILENAME”該行路徑要和站點根目錄路徑一緻。

檢測:

[root@centos-01 vhost]# curl -x127.0.0.1:80 test.com/index.php

This is a test of .php

注: 此處為了友善檢測,已将使用者認證關閉。

12.9 Nginx域名重定向

編輯虛拟主機配置檔案:

server_name test.com; test2.com;test3.com;

#為一個IP配置多個域名,此時權重會改變,是以需要使使用者通路其他域名時全部跳轉到第一個域名

if ($host != 'test.com' ) {

rewrite ^/(.*)$ http://test.com/$1 permanent;

#使用rewrite子產品

說明: 使用rewrite子產品進行域名重定向實作域名跳轉功能。

[root@centos-01 vhost]# curl -x127.0.0.1:80 test2.com -I

HTTP/1.1 301 Moved Permanently

Server: nginx/1.12.1

Date: Thu, 10 Aug 2017 10:41:30 GMT

Content-Type: text/html

Content-Length: 185

Connection: keep-alive

Location: http://test.com/

即,301:永久域名跳轉,跳轉後的位址為:Location: http://test.com/。

擴充:Nginx配置檔案詳解

#定義Nginx運作的使用者和使用者組

user www www;

#nginx程序數,建議設定為等于CPU總核心數。

worker_processes 8;

#全局錯誤日志定義類型,[ debug | info | notice | warn | error | crit ]

error_log /var/log/nginx/error.log info;

#程序檔案

pid /var/run/nginx.pid;

#一個nginx程序打開的最多檔案描述符數目,理論值應該是最多打開檔案數(系統的值ulimit -n)與nginx程序數相除,但是nginx配置設定請求并不均勻,是以建議與ulimit -n的值保持一緻。

worker_rlimit_nofile 65535;

#工作模式與連接配接數上限

#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本核心中的高性能網絡I/O模型,如果跑在FreeBSD上面,就用kqueue模型。

#單個程序最大連接配接數(最大連接配接數=連接配接數*程序數)

worker_connections 65535;

#設定http伺服器

include mime.types; #檔案擴充名與檔案類型映射表

default_type application/octet-stream; #預設檔案類型

#charset utf-8; #預設編碼

server_names_hash_bucket_size 128; #伺服器名字的hash表大小

client_header_buffer_size 32k; #上傳檔案大小限制

large_client_header_buffers 4 64k; #設定請求緩

client_max_body_size 8m; #設定請求緩

sendfile on; #開啟高效檔案傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出檔案,對于普通應用設為 on,如果用來進行下載下傳等應用磁盤IO重負載應用,可設定為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖檔顯示不正常把這個改成off。

autoindex on; #開啟目錄清單通路,合适下載下傳伺服器,預設關閉。

tcp_nopush on; #防止網絡阻塞

tcp_nodelay on; #防止網絡阻塞

keepalive_timeout 120; #長連接配接逾時時間,機關是秒

#FastCGI相關參數是為了改善網站的性能:減少資源占用,提高通路速度。下面參數看字面意思都能了解。

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffer_size 64k;

fastcgi_buffers 4 64k;

fastcgi_busy_buffers_size 128k;

fastcgi_temp_file_write_size 128k;

#gzip子產品設定

gzip on; #開啟gzip壓縮輸出

gzip_min_length 1k; #最小壓縮檔案大小

gzip_buffers 4 16k; #壓縮緩沖區

gzip_http_version 1.0; #壓縮版本(預設1.1,前端如果是squid2.5請使用1.0)

gzip_comp_level 2; #壓縮等級

gzip_types text/plain application/x-javascript text/css application/xml;

#壓縮類型,預設就已經包含text/html,是以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。

gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接配接數的時候需要使用

upstream blog.ha97.com {

#upstream的負載均衡,weight是權重,可以根據機器配置定義權重。weigth參數表示權值,權值越高被配置設定到的幾率越大。

server 192.168.80.121:80 weight=3;

server 192.168.80.122:80 weight=2;

server 192.168.80.123:80 weight=3;

#虛拟主機的配置

#監聽端口

#域名可以有多個,用空格隔開

server_name www.ha97.com ha97.com;

root /data/www/ha97;

location ~ ..(php|php5)?$

fastcgi_pass 127.0.0.1:9000;

include fastcgi.conf;

#圖檔緩存時間設定

location ~ ..(gif|jpg|jpeg|png|bmp|swf)$

expires 10d;

#JS和CSS緩存時間設定

location ~ .*.(js|css)?$

expires 1h;

#日志格式設定

log_format access '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" $http_x_forwarded_for';

#定義本虛拟主機的通路日志

access_log /var/log/nginx/ha97access.log access;

繼續閱讀