Nginx安装

十二周二次课(1月3日)
12.6 Nginx安装
wget http://nginx.org/download/nginx-1.13.8.tar.gz
tar zxf nginx-1.13.8.tar.gz
cd nginx-1.13.8
./configure –prefix=/usr/local/nginx

编辑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

[root@wwlinux701 src]# chmod 755 /etc/init.d/nginx
[root@wwlinux701 src]# chkconfig –add nginx
[root@wwlinux701 src]# chkconfig nginx on

cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak

[root@wwlinux701 src]# cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
[root@wwlinux701 conf]# vim nginx.conf  #编辑ninx配置文件
--------------------------------------------------------------------------------------------------------------------------


user nobody nobody; #定义nginx哪个用户启动
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;
        }    
    }
}

[root@wwlinux701 conf]# /usr/local/nginx/sbin/nginx -t #检查nginx配置是否正确
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@wwlinux701 conf]# /etc/init.d/nginx  start   #启动nginx
Starting nginx (via systemctl):                            [  确定  ]
[root@wwlinux701 conf]# netstat -lntp |grep 80
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5051/nginx: master  
[root@wwlinux701 conf]# 


测试解析php
vi /usr/local/nginx/html/1.php
<?php
    echo "test php scripts.";
?>
[root@wwlinux701 conf]# curl localhost/1.php
test php scripts.
[root@wwlinux701 conf]# 

这里写图片描述
12.7 默认虚拟主机

• vim /usr/local/nginx/conf/nginx.conf //增加下面一行,注释掉server段
• include vhost/*.conf;
这里写图片描述
• mkdir /usr/local/nginx/conf/vhost
• cd !$; vim default.conf //加入如下内容

server
{
    listen 80 default_server;  // 有这个标记的就是默认虚拟主机
    server_name abc.com;
    index index.html index.htm index.php;
    root /data/wwwroot/default;
}

• mkdir -p /data/wwwroot/default/
• echo “This is a default site.”>/data/wwwroot/default/index.html
• /usr/local/nginx/sbin/nginx -t
• /usr/local/nginx/sbin/nginx -s reload #重新加载nginx配置文件
• curl localhost
• curl -x127.0.0.1:80 123.com

这里写图片描述
12.8 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; 
}
}

需要用到httpd的密码生成工具

/usr/local/apache2.4/bin/htpasswd  -c /usr/local/nginx/conf/htpasswd aming #指定aming用户,后面输入两次密码
[root@wwlinux701 vhost]# cat /usr/local/nginx/conf/htpasswd
aming:$apr1$UlCtYDIY$1QlVxKmv6lhVtrUacF78i1
[root@wwlinux701 vhost]# 
 /usr/local/nginx/sbin/nginx -t
/usr/local/nginx/sbin/nginx -s reload 

如果没有安装需要先安装httpd,yum安装后可以直接敲htpasswd
• yum install -y httpd
• htpasswd -c /usr/local/nginx/conf/htpasswd aming
• -t && -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说明需要验证
• curl -uaming:passwd 访问状态码变为200
curl -uaming:1234 -x127.0.0.1:80 test.com -I
这里写图片描述
• 编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗

• 针对目录的用户认证
location /admin/
{
auth_basic “Auth”;
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
• 匹配一个页面的用户认证

location ~ admin.php
{
auth_basic “Auth”;
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
12.9 Nginx域名重定向
• 更改test.com.conf

server
{
    listen 80;
    server_name test.com test1.com test2.com;   #apache不支持多个servername,需要用别名。但是nginx就能支持多个servername
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' ) {    #如果不是test.com开头就跳转到test.com
        rewrite  ^/(.*)$  http://test.com/$1  permanent;  #permanents 301的意思
    }
}

• server_name后面支持写多个域名,这里要和httpd的做一个对比
• permanent为永久重定向,状态码为301,如果写redirect则为302

扩展
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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值