(二)Nginx安裝與配置

概述

  • 下載並安裝Nginx組件
  • 配置Nginx環境,本文不會講完,後續會繼續講
  • 1.配置默認虛擬主機。2.Nginx用戶認證。3.Nginx域名重定向

安裝 Nginx

1.下載和解壓Nginx,javascript

#進入下載目錄
cd /usr/local/src
#下載
wget http://nginx.org/download/nginx-1.12.1.tar.gz
#解壓
tar zxf nginx-1.12.1.tar.gz

2.配置編譯選項,php

#進入包裏
cd nginx-1.8.0
#配置編譯選項
./configure --prefix=/usr/local/nginx

3.編譯和安裝Nginx,css

make &&  make install

4.編寫Nginx啓動腳本,並加入系統服務,html

[root@centos7mei nginx-1.8.0]# vim /etc/init.d/nginx

配置文件詳細java

#!/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

最好檢查下node

/usr/local/nginx/sbin/nginx -t

接下來更改權限,加入開機啓動nginx

[root@centos7mei nginx-1.8.0]# chmod 755 /etc/init.d/nginx
[root@centos7mei nginx-1.8.0]# chkconfig --add nginx

5.更改Nginx配置文件vim

[root@centos7mei nginx-1.8.0]# cd /usr/local/nginx/conf/
[root@centos7mei conf]# ls
fastcgi.conf          fastcgi_params.default  mime.types          nginx.conf.default   uwsgi_params
fastcgi.conf.default  koi-utf                 mime.types.default  scgi_params          uwsgi_params.default
fastcgi_params        koi-win                 nginx.conf          scgi_params.default  win-utf
[root@centos7mei conf]# mv nginx.conf nginx.conf.bak
[root@centos7mei conf]# vim nginx.conf

配置文件詳細centos

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;
        }    
    }
}

別忘了檢查讀寫bash

[root@centos7mei 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

6.啓動Nginx

[root@centos7mei conf]# /etc/init.d/nginx  start
Starting nginx (via systemctl):                            [  OK  ]
[root@centos7mei conf]# ps aux | grep nginx
root     16489  0.0  0.0  24824   792 ?        Ss   19:26   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody   16490  0.0  0.1  27268  3376 ?        S    19:26   0:00 nginx: worker process
nobody   16491  0.0  0.1  27268  3376 ?        S    19:26   0:00 nginx: worker process
root     16497  0.0  0.0 112648   968 pts/0    R+   19:26   0:00 grep --color=auto nginx

7.測試是否正確解析PHP

[root@centos7mei conf]# vi /usr/local/nginx/html/1.php
[root@centos7mei conf]# curl localhost/1.php
This is nginx test page.[root@centos7mei conf]#

php內容

<?php
echo "This is nginx test page.";

Nginx環境配置

默認虛擬主機

  • 介紹HTTP的時候咱們學習過關於默認虛擬主機的概念,在Nginx中也有相似的東西,第一個被Nginx加載的虛擬主機就是默認主機。可是和httpd不一樣的是,它還有一個配置用來標記默認虛擬主機。也就是說沒有這個標記,第一個虛擬主機爲默認虛擬主機

1.修改主配置文件nginx.conf

[root@centos7mei vhost]# vim /usr/local/nginx/conf/nginx.conf

配置文件內容以下

[root@centos7mei vhost]# cat /usr/local/nginx/conf/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;

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;
	##這裏這裏:添加的東西就是下面這個,括號後面的刪掉刪掉,添加完記得檢查讀寫
    include vhost/*.conf;
}

這一步的意思是/usr/local/nginx/conf/vhost/下面的全部以.conf結尾的文件都會被加載,咱們就能夠把全部虛擬主機配置文件放到vhost目錄下面了。

2.添加vhost文件

[root@centos7mei conf]# mkdir /usr/local/nginx/conf/vhost
[root@centos7mei conf]# cd !$
cd /usr/local/nginx/conf/vhost
[root@centos7mei vhost]# vim default.conf

配置文件以下

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

檢查讀寫

[root@centos7mei 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

3.從新加載服務和測試

[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos7mei vhost]# curl localhost
This is a default site.
[root@centos7mei vhost]# curl -x127.0.0.1:80 123.com
This is a default site.

用戶認證

1.建立一個新的虛擬主機

(reverse-i-search)`cd': cd /usr/local/nginx/conf/vhost/
[root@centos7mei vhost]# vim 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;
}
}

2.建立用戶

[root@centos7mei vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd aming
New password: 
Re-type new password: 
Adding password for user aming
[root@centos7mei 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

3.測試

#添加依賴包
[root@centos7mei vhost]# yum install -y httpd
#從新加載服務
[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
[root@centos7mei vhost]# mkdir /data/nginx/test.com
mkdir: cannot create directory ‘/data/nginx/test.com’: No such file or directory
[root@centos7mei vhost]# mkdir -p /data/nginx/test.com
[root@centos7mei vhost]# echo 「test.com」>/data/nginx/test.com/index.html
#嘗試鏈接後顯示401,說明該網站須要用戶認證
[root@centos7mei vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.8.0
Date: Thu, 16 Aug 2018 14:53:53 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"
#輸入用戶名和密碼後,就能訪問了
[root@centos7mei vhost]# curl -uaming:123 -x127.0.0.1:80 test.com
「test.com」

4.單目錄用戶認證 剛纔的認證是針對全站的,如今咱們的需求是指定單個目錄訪問

[root@centos7mei vhost]# vi test.com.conf
#指定目錄爲/admin/
server
{
    listen 80;
    server_name test.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    
location  /admin/  ##這裏這裏,填寫目錄就能夠了
    {
        auth_basic              "Auth";
        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;
}
}
#檢查讀寫並從新加載服務
[root@centos7mei 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@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload
#如今訪問站點的時候就不須要驗證了
[root@centos7mei vhost]# curl -x127.0.0.1:80 test.com
「test.com」
#可是當咱們訪問/admin/目錄的時候就須要驗證了
[root@centos7mei vhost]# curl -x127.0.0.1:80 test.com/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.8.0</center>
</body>
</html>

域名重定向

  • 需求:有多個網址,訪客訪問的不是主站,就要用域名重定向把它跳轉到主站去

1.修改配置文件

[root@centos7mei vhost]# vim test.com.conf
[root@centos7mei 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
#配置文件,這裏咱們只留了域名重定向的配置文件
server
{
    listen 80;
    server_name test.com test2.com test3.com;
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {
        rewrite  ^/(.*)$  http://test.com/$1  permanent;
    }
}
#從新加載服務
[root@centos7mei vhost]# /usr/local/nginx/sbin/nginx -s reload

2.測試

#訪問站點顯示301,301不是錯誤代碼,是域名重定向
[root@centos7mei vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 16 Aug 2018 16:03:09 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html
#加上字符串後仍是顯示301
[root@centos7mei vhost]# curl -x127.0.0.1:80 test2.com/index.html/SDFAS -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 16 Aug 2018 16:03:27 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html/SDFAS
[root@centos7mei vhost]# curl -x127.0.0.1:80 test3.com/index.html/SDFAS -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.8.0
Date: Thu, 16 Aug 2018 16:03:37 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: http://test.com/index.html/SDFAS
##這裏顯示404,是由於咱們配置文件裏沒有這個網站,就直接跳到默認虛擬主機了
[root@centos7mei vhost]# curl -x127.0.0.1:80 test4.com/index.html/SDFAS -I
HTTP/1.1 404 Not Found
Server: nginx/1.8.0
Date: Thu, 16 Aug 2018 16:03:47 GMT
Content-Type: text/html
Content-Length: 168
Connection: keep-alive

擴展

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

相關文章
相關標籤/搜索