11-4 12 Nginx安裝 默認虛擬主機 用戶認證 域名重定向

12.6 Nginx安裝javascript

12.7 默認虛擬主機php

12.8 Nginx用戶認證css

12.9 Nginx域名重定向html

擴展前端

nginx.conf 配置詳解 http://www.ha97.com/5194.htmljava

http://my.oschina.net/duxuefeng/blog/34880node

12.6 Nginx安裝

  1. 下載
  2. 安裝
  3. 配置
  4. 啓動服務

第一步:下載nginx

[root@axiang-02 nginx]# cd /usr/local/src
[root@axiang-02 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
[root@axiang-02 src]#  tar zxf nginx-1.12.1.tar.gz

第二步:安裝apache

[root@axiang-02 src]# cd nginx-1.12.1/
[root@axiang-02 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx ^C
#若是須要支持某些模塊,能夠再次添加,好比https ssl等
[root@axiang-02 nginx-1.12.1]# make &&  make install
[root@axiang-02 nginx-1.12.1]# echo $?

第三步:配置vim

[root@axiang-02 nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@axiang-02 conf]# mv nginx.conf nginx.conf.bak ^C
[root@axiang-02 conf]# vim nginx.conf //D15Z/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@axiang-02 conf]# vim /etc/init.d/nginx   //建立啓動服務腳本 D15Z/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
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

[root@axiang-02 conf]# chmod 755 /etc/init.d/nginx 
[root@axiang-02 conf]# chkconfig --add nginx
[root@axiang-02 conf]# chkconfig nginx on
[root@axiang-02 conf]# /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@axiang-02 conf]# /etc/init.d/nginx start
[root@axiang-02 conf]# netstat -lntp | grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1122/nginx: master

Nginx安裝完成!

  • Ss爲父進程,通常爲root下
  • S爲子進程

測試

vi /usr/local/nginx/html/1.php 
     <?php
    echo "This is nginx page.";
    ?>
     curl localhost/1.php

常見的502問題解決

對於LNMP來講,最多見的就是502問題,LNMP環境搭建完成後,一訪問網站直接提示「502 Bad Gateway」。主要緣由大體分爲兩種:

(1)配置錯誤

在Nginx配置中有這麼一段:

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; } 若是把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,以此類推。

若是遇到其它的較爲少見的錯誤,咱們能夠修改nginx的錯誤日誌(/usr/local/nginx/logs/nginx_error.log)的級別,在配置文件/usr/local/nginx/conf/nginx.conf中將crit改成debug,使其記錄最多的日誌內容,這樣方便咱們排查錯誤,可是配置更改完成後要記得將級別改回crit,避免日誌文件佔用太多磁盤空間。

12.7 Nginx默認虛擬主機

[root@axiang-02 conf]# vim nginx.conf
刪除下面的server配置,加入一行:
include vhost/*.conf;

[root@axiang-02 conf]# mkdir vhost
[root@axiang-02 conf]cd !$;  vim aaa.conf //加入如下內容
server
{
    listen 80 default_server;  // 有這個標記的就是默認虛擬主機
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/aaa;
}
[root@axiang-02 vhost]# mkdir -p /data/wwwroot/aaa/
[root@axiang-02 vhost]# echo 「This is a default site.」>/data/wwwroot/aaa/index.html
[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -t
[root@axiang-02 vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@axiang-02 vhost]# curl localhost
[root@axiang-02 vhost]# curl -x127.0.0.1:80 23sdsa.com

  • 若是沒有default_server,則按字符順序尋找vhost下虛擬主機,例如 aaa在bbb前
  • 若是vhost定義了兩個default_server,則報錯
  • 直接curl aaa.com 沒有返回
  • 路徑寫錯好比wwwoot則404!

12.8 Nginx用戶認證

[root@axiang-02 vhost]# vim bbb.conf  //加入以下內容
...
server
{
    listen 80 default_server;  //注意只能定義一個默認虛擬主機,須要把aaa的刪掉
    server_name bbb.com;
    index index.html index.htm index.php;
    root /data/wwwroot/bbb.com;

location  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
    }
}
...
 yum install -y httpd  
 htpasswd -c /usr/local/nginx/conf/htpasswd ax  //也能夠用apache2.4/bin下的htpasswd生成

-t && -s reload //測試配置並從新加載

  • -t測試不經過沒法reload

定義了多個默認虛擬主機產生衝突

域名衝突,檢查其餘conf是否重複定義同一個域名

[root@axiang-02 vhost]# mkdir /data/wwwroot/bbb.com
[root@axiang-02 vhost]# echo 「This is bbb.com page」>/data/wwwroot/bbb.com/index.html
[root@axiang-02 vhost]# curl -x127.0.0.1:80 bbb.com -I//狀態碼爲401說明須要驗證
[root@axiang-02 vhost]# curl -uaming:passwd 訪問狀態碼變爲200

針對目錄及頁面的用戶認證

location  /admin/
location ~ admin.php  //沒有該頁面也會提示401,輸入用戶密碼則404

12.9 Nginx域名重定向

更改ccc.conf

server
{
    listen 80;
    server_name ccc.com ccc1.com ccc2.com;
    index index.html index.htm index.php;
    root /data/wwwroot/ccc.com;
    if ($host != 'test.com' )
    {
        rewrite  ^/(.*)$  http://ccc.com/$1  permanent;
    }
}

測試效果

  • server_name後面支持寫多個域名,httpd寫在server_alias後面
  • permanent爲永久重定向,狀態碼爲301,SEO權重高,若是寫redirect則爲302
  • 謂默認虛擬主機就是/usr/local/nginx/conf/vhost目錄下虛擬主機配置文件中有「default_server」標記的虛擬主機

擴展: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;

#工做模式與鏈接數上限
events
{
#參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,若是跑在FreeBSD上面,就用kqueue模型。
use epoll;
#單個進程最大鏈接數(最大鏈接數=鏈接數*進程數)
worker_connections 65535;
}

#設定http服務器
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
{
#監聽端口
listen 80;
#域名能夠有多個,用空格隔開
server_name www.ha97.com ha97.com;
index index.html index.htm index.php;
root /data/www/ha97;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
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;

#對 "/" 啓用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#如下是一些反向代理的配置,可選。
proxy_set_header Host $host;
client_max_body_size 10m; #容許客戶端請求的最大單文件字節數
client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數,
proxy_connect_timeout 90; #nginx跟後端服務器鏈接超時時間(代理鏈接超時)
proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時)
proxy_read_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時)
proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下的設置
proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#設定緩存文件夾大小,大於這個值,將從upstream服務器傳
}

#設定查看Nginx狀態的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd文件的內容能夠用apache提供的htpasswd工具來產生。
}

#本地動靜分離反向代理配置
#全部jsp的頁面均交由tomcat或resin處理
location ~ .(jsp|jspx|do)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:8080;
}
#全部靜態文件由nginx直接讀取不通過tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}

本節來自:http://www.ha97.com/5194.html 詳參:http://wiki.nginx.org/Main

相關文章
相關標籤/搜索