Nginx安裝, 默認虛擬主機,Nginx用戶認證,Nginx域名重定向

Nginx安裝:

 

cd /usr/local/srcjavascript

wget http://nginx.org/download/nginx-1.12.1.tar.gz       =nginx下載地址(或者直接能夠去官網下載)php

tar zxf nginx-1.12.1.tar.gz           = 解壓下載包css

cd nginx-1.12.1           = 進入解壓好的目錄html

./configure --prefix=/usr/local/nginx    =編譯nginx   (編譯時能夠根據需求添加須要的模塊)java

make &&  make install         = 繼續安裝node

vim /etc/init.d/nginx //複製以下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx?public=true )            = 編輯啓動腳本linux

#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           = 加入開機服務自啓動git

chkconfig nginx on       = 開啓開機服務自啓動      on=開啓     off=關閉vim

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak         = 進入nginx配置目錄給本來的配置文件更改一個名字

vim nginx.conf //寫入以下內容(參考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)          =  寫入本身的配置文件

#nginx配置文件
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;
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$ 
        {
            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

netstat -lntp |grep 80              =  查看端口    nginx監聽80端口

systemctl status nginx.service    = 若是啓動nginx 出問題能夠使用命令來判斷問題點在哪

 

vi /usr/local/nginx/html/1.php      //加入以下內容
 <?php
    echo "test php scripts.";
?>
 curl localhost/1.php

 

Nginx默認虛擬主機:

vim /usr/local/nginx/conf/nginx.conf //增長     = 更改原來的配置文件

include vhost/*.conf             =   增長新定義的配置文件

mkdir /usr/local/nginx/conf/vhost               =在conf目錄下新建立一個vhost目錄

cd !$;  vim default.conf //加入以下內容        = vhost目錄下default.conf 文件裏面寫入新配置     !$=上一條執行的命令

#配置文件
server
{
    listen 80 default_server;  // 有這個標記的就是默認虛擬主機
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

mkdir -p /data/wwwroot/default/          = 配置裏面定義的網站根目錄 (若是有那麼就能夠不用建立)

cd /data/wwwroot/default/   = 進入到建立的網站根目錄隨便寫點東西測試使用

vim  index.html    =定義一個測試的配置文件 echo 「This is a default site.」

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 localhost             = 測試解析本機

curl -x127.0.0.1:80 123.com     = 測試解析其餘域名

 

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;          =定義用戶密碼文件
}
}

yum install -y httpd             = 工具需求安裝

htpasswd -c /usr/local/nginx/conf/htpasswd aming              =生成一個用戶名和密碼文件(若是還須要繼續生成第二個用戶和密碼那麼就不用加 -c )

   cat 能夠查看用生成的用戶和密碼

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

[root@aming-01 vhost]# /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@aming-01 vhost]# /usr/local/nginx/sbin/nginx -s reload     =  從新加載配置文件

mkdir /data/wwwroot/test.com     = 建立一個測試配置文件目錄

echo 「test.com」>/data/wwwroot/test.com/index.html    = 建立的測試配置文件

curl -x127.0.0.1:80 test.com -I//狀態碼爲401說明須要驗證            = 測試  出現401說明須要認證用戶

curl -uaming:passwd 訪問狀態碼變爲200         =  認證用戶後測試狀態爲200

[root@aming-01 vhost]# curl -uaming:rabbit -x127.0.0.1:80 test.com
「test.com」

編輯windows的hosts文件,而後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗針對目錄的用戶認證

location  /admin/
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}

 

Nginx域名重定向:

更改test.com.conf

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

server_name後面支持寫多個域名,這裏要和httpd的作一個對比

permanent爲永久重定向,狀態碼爲301,若是寫redirect則爲302

 

擴展

nginx.conf 配置詳解 http://www.ha97.com/5194.htmlhttp://my.oschina.net/duxuefeng/blog/34880
nginx rewrite四種flag 

http://www.netingcn.com/nginx-rewrite-flag.htmlhttp://unixman.blog.51cto.com/10163040/1711943

相關文章
相關標籤/搜索