(二)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