6月7日任务 Nginx安装、 默认虚拟主机、Nginx用户认证、Nginx域名重定向

12.6 Nginx安装php

1. wget http://nginx.org/download/nginx-1.12.1.tar.gz   //下载安装包html

2. tar -zxvf nginx-1.12.1.tar.gz    //解压文件linux

3.  ./configure --prefix=/usr/local/nginx   //配置nginx

4.  make && make install   //编译安装git

5.  设置启动文件  apache

#!/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浏览器

6. chmod 755 /etc/init.d/nginx    //设置权限缓存

7. chkconfig --add nginx     chkconfig  nginx  on    //设置开机启动bash

8. cd /usr/local/nginx/conf/nginx.conf    //配置文件url

配置文件见: 

https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf?public=true

/usr/local/nginx/sbin/nginx -t    //检测语法是否正确

9.  /etc/init.d/nginx start   //启动

 注 :  若是解析不了php  请检查配置文件 与php-rpm一致    

 

 

12.7 Nginx默认虚拟主机

1. 配置文件增长 include vhost/*.conf;    删除虚拟主机默认配置信息     //定义配置虚拟主机

2. 建立 vhost目录 和vhost.conf     

3. 编辑vhost.conf

server

{

    listen 80 default_server;  // 有这个标记的就是默认虚拟主机

    server_name aaa.com;

    index index.html index.htm index.php;

    root /data/wwwroot/default;

}

4. /usr/local/nginx/sbin/nginx -s reload

错误现象: php返回200 但页面空白      配置文件错误

server
    {
        listen 80 default_server;
        server_name localhost;
        index index.html index.htm index.php;
        root  /data/wwwroot/111;
location ~ \.php$ {
         root           html;      //必须存在
         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include        fastcgi_params;  //必须存在

 

或者   server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/111;
 location ~ \.php$
{
root   /data/wwwroot/111;
fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include    fastcgi.conf;

12.8 Nginx用户认证

1. 修改配置文件 

location  /

    {

        auth_basic              "lxy";     //用户名

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;  //密码文件

2. yum install -y  httpd  // 用httpd 生成

3. 生成密码文件  

/usr/local/apache/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lxy

4. 针对目录验证

location  /data/wwwroot/111

    {

        auth_basic              "lxy";     //用户名

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;  //密码文件

5. 针对访问的url 

location  ~ index.php

    {

        auth_basic              "lxy";     //用户名

        auth_basic_user_file   /usr/local/nginx/conf/htpasswd;  //密码文件

 

12.9 Nginx域名重定向

1. 修改配置文件

server
{
    listen 80 default_server;
    server_name aaa.com;
    index index.html index.htm index.php;
    root /data/wwwroot/111;
if ( $host != ‘111.com’ ){
rewrite ^(.*)$ http://111.com$1 permanent;      //301     redirect  302

 

301适合永久重定向

  301比较经常使用的场景是使用域名跳转。

  好比,咱们访问 http://www.baidu.com 会跳转到 https://www.baidu.com,发送请求以后,就会返回301状态码,而后返回一个location,提示新的地址,浏览器就会拿着这个新的地址去访问。 

  注意: 301请求是能够缓存的, 即经过看status code,能够发现后面写着from cache。

     或者你把你的网页的名称从php修改成了html,这个过程当中,也会发生永久重定向。

 

302用来作临时跳转

  好比未登录的用户访问用户中心重定向到登陆页面。

  访问404页面会从新定向到首页。 

相关文章
相关标签/搜索