005.nginx配置文件

1.替換nginx主配置文件javascript

經過前面的配置,LNMP的環境已經搭建完成,如今咱們替換nginx配置文件:php

[root@huh ~]# cd /usr/local/nginx/conf/
[root@huh conf]# > nginx.conf
[root@huh conf]# vim nginx.conf

寫入後的nginx.conf:css

#定義Nginx運行的用戶和用戶組,系統中必須有此用戶,能夠是nologin
user nobody nobody;

#啓動進程,一般設置成和cpu的數量相等,(設置爲「auto」將嘗試自動檢測它)
worker_processes 2;

#全局錯誤日誌 [debug | info | notice | warn |error | crit]
error_log /usr/local/nginx/logs/nginx_error.log crit;

#進程PID文件
pid /usr/local/nginx/logs/nginx.pid;

#一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,可是nginx分配請求並不均勻,因此建議與ulimit -n的值保持一致。
worker_rlimit_nofile 1024;
#
#
#工做模式與鏈接數上限
events
{
    #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可是僅用於linux2.6以上內核,能夠大大提升nginx的性能(若是跑在FreeBSD上,就用kqueue模型)
    use epoll;
    #單個後臺worker process進程的最大併發連接數 (最大鏈接數=鏈接數*進程數)
    worker_connections 1024; 
}
#
#
#設定http服務器,利用它的反向代理功能提供負載均衡支持
http
{
    #設定mime類型,類型由mime.type文件定義
    include mime.types;
    #默認文件類型
    default_type application/octet-stream;
    #默認編碼
    #charset utf-8; 
    #服務器名字的hash表大小
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    #log_format用來設置日誌格式
    log_format huh '$remote_addr $http_x_forwarded_for [$time_local]'
    '$host "$request_uri" $status'
    '"$http_referer" "$http_user_agent"';
    #sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,
    #必須設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,下降系統的uptime.
    sendfile on;
    #防止網絡阻塞
    tcp_nopush on;
    #鏈接超時時間(單位是秒)
    keepalive_timeout 30;
    #請求頭的超時時間
    client_header_timeout 3m;
    #請求體的超時時間
    client_body_timeout 3m;
    #客戶端的響應超時時間,這個設置不會用於整個轉發器,而是在兩次客戶端讀取操做之間。若是在這段時間內,客戶端沒有讀取任何數據,nginx就會關閉鏈接。
    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壓縮
    gzip on;
    #最小壓縮文件大小
    gzip_min_length 1k;
    #壓縮緩衝區
    gzip_buffers 4 8k;
    #壓縮等級
    gzip_comp_level 5;
    #壓縮版本(默認1.1,前端若是是squid2.5請使用1.0)
    gzip_http_version 1.1;
    #壓縮類型,默認就已經包含text/html,因此下面就不用再寫了,寫上去也不會有問題,可是會有一個warn。
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
     
    #包含其它配置文件,如自定義的虛擬主機
    include vhosts/*.conf;
}

 

2.添加nginx虛擬主機配置文件html

咱們建立vhosts文件夾:前端

[root@huh conf]# mkdir vhosts
[root@huh conf]# cd vhosts

寫入默認虛擬主機配置文件java

[root@huh vhosts]# vim default.conf

寫入內容:node

#默認的虛擬主機配置文件
server
{
    #一樣監聽80端口
    listen 80 default;
    #定義使用localhost訪問
    server_name localhost;
    #默認訪問的界面
    index index.html index.htm index.php;
    #該域名能夠訪問的根目錄,(咱們能夠給該目錄加訪問權限,讓全部人都沒法訪問!) 
    root /tmp/1233;
    #禁止全部IP訪問!(也就是說誰也不能訪問默認虛擬主機配置文件訪問此網站)
    deny all;
}

 

 3.寫入虛擬主機配置文件test.conf:linux

[root@huh vhosts]# vim test.conf

寫入內容:nginx

#虛擬主機配置文件

server
{
    #監聽端口
    listen 80;
    #定義使用www.xxx.com訪問
    server_name www.test.com www.aaa.com www.bbb.com;
    #設定本虛擬主機的訪問日誌,(huh爲日誌格式,在nginx主配置文件中定義)
    access_log /tmp/access.log huh;
    
    #IP訪問控制,禁止某些IP訪問網站
    #deny 10.0.0.83;
   
    if ($http_user_agent ~ 'baidu|111111')     
    {         
        return 403;
    }

    #nginx域名跳轉,(permanent表明永久重定向,返回http狀態301)
    if ($host != 'www.test.com')
    {
        rewrite ^/(.*)$ http://www.test.com/$1 permanent;
    }

    index index.html index.htm index.php;
    #該域名能夠訪問的根目錄
    root /data/www;
    #對指定的目錄添加用戶認證模塊    
    location ~ .*admin\.php$ {
        #對該目錄容許訪問的IP
        allow 10.0.0.83;
        allow 127.0.0.1;
        #對該目錄禁止其它IP訪問
        deny all;
        #認證提示
        auth_basic "huh's auth";
        #認證所需的"保存用戶密碼的文件"
        auth_basic_user_file /usr/local/nginx/conf/.htpasswd;
        
        #如下部分爲解析php所需
        #將fastcgi_params配置文件包含進來
        include fastcgi_params;
        #fastcgi_pass設置使用何種方式鏈接php,(php進程使用何種方式監聽,這兒也要設置使用何種方式鏈接,)
        fastcgi_pass unix:/tmp/www.sock;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        #腳本文件請求的路徑,其中$fastcgi_script_name是請求腳本的名稱。
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }

    #設置不記錄指定文件類型日誌
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|mp3|mp4|avi|flv|rar|zip|gz|bz2)$
    {
        #不記錄符合此匹配規則的文件
        access_log off;
        #設置靜態文件過時時間
        expires 15d;
        #配置防盜鏈,其中invalid_referer是對valid_referers的否認形式
        valid_referers none blocked *.test.com *.aaa.com *.aminglinux.com;         
        if ($invalid_referer)         
        {             
            return 403;          
        }   
    }
    
    #設置不記錄指定文件類型日誌
    location ~ (static|cache)
    {
        access_log off;
        expires 2h;
    }
    
    #設置解析php
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/www.sock;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
    }
}

注:apache

1./usr/local/nginx/conf/.htpasswd這個加密文件須要使用apache裏面的htpasswd工具去生成,因此使用這個工具須要先安裝apache!

假如咱們的apache安裝在/usr/local/apache2/目錄下,則:

[root@huh vhosts]# cd ..
[root@huh conf]# /usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd huh
New password: 
Re-type new password: 
Adding password for user huh

注:使用-c參數建立該文件,若文件已存在,則不加-c

 

4.查看配置文件是否正確

[root@huh vhosts]# /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@huh vhosts]# /usr/local/nginx/sbin/nginx -s reload

訪問discuz:

咱們去主機的hosts文件(不是虛擬機)中添加一條dns:

[root@huh ~]# vim /etc/hosts
最末行添加的語句(假如當前虛擬機的IP是10.0.0.126):
10.0.0.126   www.test.com www.aaa.com www.bbb.com

在瀏覽器中輸入域名訪問:http://www.test.com

訪問成功!說明咱們配置文件都是正確的!

相關文章
相關標籤/搜索