天天看點

[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

Nginx安裝

  • 進入存放源碼包的目錄: cd /usr/local/src
  • 下載下傳源碼包: wget http://nginx.org/download/nginx-1.12.1.tar.gz
  • 解壓: tar zxf nginx-1.12.1.tar.gz
  • 安裝Nginx:
./configure --prefix=/usr/local/nginx      
  • 編譯安裝: make && make install
  • 編輯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
    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      
  • 修改啟動腳本的權限: chmod 755 /etc/init.d/nginx
  • 添加Nginx服務: chkconfig –add nginx
  • 設定開機啟動: chkconfig nginx on
  • 配置Nginx的配置檔案,因為Nginx下已經有nginx.conf這個配置檔案,我們不用它,把它備份起來然後用自己的配置: cd /usr/local/nginx/conf/ mv nginx.conf nginx.conf.bak
  • 編輯配置檔案: vim nginx.conf 寫入如下内容
user nobody nobody;
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 //每一個server對應一個虛拟主機
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.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;
        }    
    }
}      
  • 測試配置檔案文法: /usr/local/nginx/sbin/nginx -t
  • 開啟Nginx: /etc/init.d/nginx start
  • Nginx對應的是80端口: netstat -lntp |grep 80
  • 測試Nginx解析php: vi /usr/local/nginx/html/1.php 加入如下内容:
<?php
    echo "test php scripts.";
?>      
  • 用curl測試: curl localhost/1.php
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

Nginx預設虛拟主機

  • 編輯配置檔案: vim /usr/local/nginx/conf/nginx.conf 增加:
include vhost/*.conf      
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向
  • 建立vhost目錄: mkdir /usr/local/nginx/conf/vhost
  • 進入vhost目錄下并建立編輯一個.conf檔案: cd /usr/local/nginx/conf/vhost vim aaa.com.conf 加入如下内容
server
{
    listen 80 default_server;  // 有這個标記的就是預設虛拟主機
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}      
  • 建立default目錄: mkdir /data/wwwroot/default/
  • 寫一些内容在default目錄下的index.html檔案中: echo “This is a default site.”>/data/wwwroot/default/index.html
  • 測試文法: /usr/local/nginx/sbin/nginx -t
  • 重新加載配置檔案,不需要重新開機服務: /usr/local/nginx/sbin/nginx -s reload
  • curl測試: curl localhost curl -x127.0.0.1:80 123.com 就算通路的不是aaa.com,隻要解析過來,指向到我們伺服器,都能通路到這個站點,這就是預設虛拟主機。
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

Nginx使用者認證

  • 編輯一個配置檔案: vim /usr/local/nginx/conf/vhost/test.com.conf 寫入如下内容:
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;

location  /
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}      
  • 建立test.com目錄: mkdir /data/wwwroot/test.com
  • 在test.com目錄下編輯index.html: echo “test.com”>/data/wwwroot/test.com/index.html
  • 如果之前沒有安裝過Apache的話就安裝httpd,為了是可以使用Apache的htpasswd工具: * yum install -y httpd*
  • Apache自帶指令htpasswd建立密碼檔案,-c是建立,-m是指定md5加密類型,指定使用者為xie(PS:如果再次新增使用者,就不需要再加 -c ,因為已經建立過密碼檔案了): /usr/local/apache2.4/bin/htpasswd -c /usr/local/nginx/conf/htpasswd xie
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向
  • 測試配置并重新加載: /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
  • curl測試: curl -x127.0.0.1:80 test.com 這裡出現401,說明需要使用者密碼認證
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

當我們用-u加上使用者名和密碼時就可以通路了

[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向
  • 針對目錄的使用者認證: 在location後面加上目錄名字即可
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

用curl測試,通路網站時是正常的,隻有通路admin目錄下時就會出現401,需要使用者認證

[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

當我們指定使用者密碼後就可以正常通路了

[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向
  • 針對檔案的使用者認證: 在location後面加上比對檔案名字,下圖的意思就是比對到admin.php這個檔案就需要使用者認證
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

curl測試,通路admin目錄時是正常的,通路admin.php就需要使用者認證了

[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

Nginx域名重定向

  • 更改test.com.conf
server
{
    listen 80;
    server_name test.com test1.com test2.com;
    //server_name後面支援寫多個域名,這裡要和httpd的做一個對比
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent; 
        //permanent為永久重定向,狀态碼為301,如果寫redirect則為302
    }
}      
  • 設定好後-t,-s測試加載配置檔案
  • curl測試: 通路test2.com後會跳轉到test.com
[轉] linux學習第四十四篇:Nginx安裝,Nginx預設虛拟主機,Nginx域名重定向

擴充 nginx.conf 配置詳解 http://www.ha97.com/5194.html http://my.oschina.net/duxuefeng/blog/34880 nginx rewrite四種flag http://www.netingcn.com/nginx-rewrite-flag.html http://unixman.blog.51cto.com/10163040/1711943

繼續閱讀