12.6 Nginx安装

  • 安装包下载到/usr/local/src目录
[root@taoyuan ~]# cd /usr/local/src
[root@taoyuan src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
  • 解压压缩包
[root@taoyuan src]# tar zxf nginx-1.12.1.tar.gz
[root@taoyuan src]# ls
nginx-1.12.1
  • 编译Nginx
[root@taoyuan nginx-1.12.1]# ./configure --prefix=/usr/local/nginx
#在此编译过程中没有加相关的参数
#需要根据需求加相关的模块[root@taoyuan nginx-1.12.1]# make && make install
#在安装之后尽量保留源码包
  • 配置Nginx
#配置文件
[root@taoyuan nginx-1.12.1]# ls /usr/local/nginx/conf/
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default#样例模板
[root@taoyuan nginx-1.12.1]# ls /usr/local/nginx/html/
50x.html  index.html #样例#存放日志
[root@taoyuan nginx-1.12.1]# ls /usr/local/nginx/logs/#Nginx进程
[root@taoyuan nginx-1.12.1]# ls /usr/local/nginx/sbin/
nginx
#支持参数
# -t 查看配置文件是否有错
  • 配置启动脚本
[root@taoyuan nginx-1.12.1]# vim /etc/init.d/nginx#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_tempdaemon $NGINX_SBIN -c $NGINX_CONFRETVAL=$?echoreturn $RETVAL
}
stop()
{echo -n $"Stopping $prog: "killproc -p $NGINX_PID $NGINX_SBIN -TERMrm -rf /dev/shm/nginx_tempRETVAL=$?echoreturn $RETVAL
}
reload()
{echo -n $"Reloading $prog: "killproc -p $NGINX_PID $NGINX_SBIN -HUPRETVAL=$?echoreturn $RETVAL
}
restart()
{stopstart
}
configtest()
{$NGINX_SBIN -c $NGINX_CONF -treturn 0
}
case "$1" instart)start;;stop)stop;;reload)reload;;restart)restart;;configtest)configtest;;*)echo $"Usage: $0 {start|stop|reload|restart|configtest}"RETVAL=1
esac
exit $RETVAL
  • 设置权限 添加服务
[root@taoyuan nginx-1.12.1]# chmod 755 /etc/init.d/nginx
[root@taoyuan nginx-1.12.1]# chkconfig --add nginx
[root@taoyuan nginx-1.12.1]# chkconfig nginx on
  • 配置文件
[root@taoyuan nginx-1.12.1]# cd /usr/local/nginx/conf/
[root@taoyuan conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
#配置文件模板#或创建新的配置文件 备份现有的文件
#使用如下的内容
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;}    }
}
  • 检测 参数 -t
[root@taoyuan 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
  • 启动服务
[root@taoyuan conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@taoyuan conf]# ps aux |grep nginx
root       6978  0.0  0.0  20496   624 ?        Ss   20:53   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     6979  0.0  0.1  22940  3212 ?        S    20:53   0:00 nginx: worker process
nobody     6980  0.0  0.1  22940  3212 ?        S    20:53   0:00 nginx: worker process
root       6982  0.0  0.0 112676   980 pts/0    S+   20:54   0:00 grep --color=auto nginx
  • 测试
  • 测试解析PHP
[root@taoyuan conf]# vim /usr/local/nginx/html/1.php
[root@taoyuan conf]# curl localhost/1.php
Hello Nginx ![root@taoyuan conf]#

12.7 默认虚拟主机

在Nginx中也有默认虚拟主机,跟httpd类似,第一个被Nginx加载的虚拟主机就是默认主机,但和httpd不相同的地方是,它还有一个配置用来标记默认虚拟主机,也就是说,如果没有这个标记,第一个虚拟主机为默认虚拟主机。

#注释掉nginx.conf 中的server 并增加一行 include vhost/*.conf
#如下是配置文件include vhost/*.conf; #增加一行#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;#   }#}
  • 创建vhost目录和配置文件
[root@taoyuan conf]# mkdir vhost
[root@taoyuan conf]# vim vhost/aaa.com.conf#配置文件内容
server
{listen 80 default_server;   #有这个标记的就是默认虚拟主机server_name aaa.com;index index.html index.htm index.php;root /data/wwwroot/default;
}
  • 创建/data/wwwroot/default
[root@taoyuan vhost]# mkdir -p /data/wwwroot/default
#如果有可以不用创建
#在目录下创建一个1.html 文件进行测试
  • 检测
#检测错误
[root@taoyuan default]# /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@taoyuan default]# /etc/init.d/nginx restart#重新加载
[root@taoyuan default]# /usr/local/nginx/sbin/nginx -s reload#测试结果
[root@taoyuan default]# curl localhost
Hello default . #默认虚拟主机
#它的域名也是跳转到默认虚拟主机
[root@taoyuan default]# curl -x127.0.0.1:80 bbb.com
Hello default .

12.8 Nginx用户认证

  • 创建一个test.com.conf 文件
[root@taoyuan default]# 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;}
}
  • 创建密码文件
#可以用Apache htpasswd工具生成
[root@taoyuan vhost]# /usr/local/apache2.4/bin/htpasswd#没有安装可以yum安装一个
[root@taoyuan vhost]# yum install -y httpd#生成文件 如果继续添加 不用带 -c选项
[root@taoyuan vhost]# htpasswd -c /usr/local/nginx/conf/htpasswd taoyuan
New password:
Re-type new password:
Adding password for user taoyuan#htpasswd 文件内容
[root@taoyuan vhost]# cat /usr/local/nginx/conf/htpasswd
taoyuan:$apr1$O4dujhQC$5XXU.vdIxoQFBeHKGWjOZ1
user:$apr1$RiNQMrIS$7RUcVzpXr3uJ5mvioNtHj/
  • 测试配置并重新加载
[root@taoyuan 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@taoyuan vhost]# /usr/local/nginx/sbin/nginx -s reload
#重新加载,不会加载配置文件中的错误。
#如果重启服务,配置文件中出现错误,将导致服务无法启动。#测试
[root@taoyuan vhost]# curl -x127.0.0.1:80 test.com -I
HTTP/1.1 401 Unauthorized
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 13:57:48 GMT
Content-Type: text/html
Content-Length: 195
Connection: keep-alive
WWW-Authenticate: Basic realm="Auth"[root@taoyuan vhost]# mkdir /data/wwwroot/test.com
[root@taoyuan vhost]# echo "test.com" > /data/wwwroot/test.com/index.html
[root@taoyuan vhost]# curl -u taoyuan:taoyuan -x127.0.0.1:80 test.com
test.com
  • 指定目录来访问控制
    当需站点需要访问某个目录才进行访问控制,只需要在 location /admin/ 增加一个目录

  • 测试
[root@taoyuan 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@taoyuan vhost]# /usr/local/nginx/sbin/nginx -s reload[root@taoyuan vhost]# curl -x127.0.0.1:80 test.com
test.com
[root@taoyuan vhost]# mkdir /data/wwwroot/test.com/admin
[root@taoyuan vhost]# echo "test.com admin dir" > /data/wwwroot/test.com/admin/index.html#curl测试
[root@taoyuan 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.12.1</center>
</body>
</html>
[root@taoyuan vhost]# curl -utaoyuan:taoyuan -x127.0.0.1:80 test.com/admin/
test.com admin dir
  • 指定一个文件访问控制


通过匹配 admin.php文件来访问控制

  • 测试
[root@taoyuan 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@taoyuan vhost]# /usr/local/nginx/sbin/nginx -s reload[root@taoyuan vhost]# echo ' <?php echo "test.com/damin.php"; ?> ' > /data/wwwroot/test.com/admin.php[root@taoyuan vhost]# curl -x127.0.0.1:80 test.com/admin/
test.com admin dir[root@taoyuan vhost]# curl -x127.0.0.1:80 test.com/admin.php
<html>
<head><title>401 Authorization Required</title></head>
<body bgcolor="white">
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.12.1</center>
</body>
</html>[root@taoyuan vhost]# curl -utaoyuan:taoyuan -x127.0.0.1:80 test.com/admin.php<?php echo "test.com/damin.php"; ?>
#由于没有配置PHP解析所以无法解析php,访问控制是配置成功的。

12.9 Nginx域名重定向

[root@taoyuan vhost]# vim /usr/local/nginx/conf/vhost/test.com.conf #配置文件编辑内容
server
{listen 80;server_name test.com test2.com test3.com; #可以跟多个域名index index.html index.htm index.php;root /data/wwwroot/test.com;#需要增加if语句进行判断if ($host != 'test.com'){rewrite ^/(.*)$ http://test.com/$1 permanent; #简写#写全为 rewrite http://$host/(.*)$ http://test.com/$1 permanent;}}
  • 检测 && 重载
[root@taoyuan 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@taoyuan vhost]# /usr/local/nginx/sbin/nginx -s reload
  • 测试结果
[root@taoyuan vhost]# curl -x127.0.0.1:80 test2.com/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 14:36:45 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/index.html[root@taoyuan vhost]# curl -x127.0.0.1:80 test2.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 14:37:06 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html[root@taoyuan vhost]# curl -x127.0.0.1:80 test3.com/admin/index.html -I
HTTP/1.1 301 Moved Permanently
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 14:37:30 GMT
Content-Type: text/html
Content-Length: 185
Connection: keep-alive
Location: http://test.com/admin/index.html[root@taoyuan vhost]# curl -x127.0.0.1:80 test4.com/admin/index.html -I
HTTP/1.1 404 Not Found
Server: nginx/1.12.1
Date: Wed, 03 Jan 2018 14:37:44 GMT
Content-Type: text/html
Content-Length: 169
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

转载于:https://blog.51cto.com/3622288/2057189

Nginx安装、默认虚拟主机、Nginx用户认证、Nginx域名重定向相关推荐

  1. 12.6-12.9 Nginx安装,默认虚拟主机,用户认证,域名重定向

    12.6 Nginx安装 大纲 1 进入src目录,把nginx下载在此目录 #cd  /usr/local/src #wget http://nginx.org/download/nginx-1.8 ...

  2. linux的Nginx安装、默认虚拟主机、用户认证、域名重定向配置介绍

    Nginx介绍 Nginx官网(http://nginx.org),最新版1.13,最新稳定版1.12 Nginx应用场景:web服务.反向代理.负载均衡 Nginx著名分支,淘宝基于Nginx开发的 ...

  3. php nginx 域名重定向,Nginx默认虚拟主机、用户认证、域名重定向

    Nginx默认虚拟主机定义默认虚拟主机配置文件,在http下面加入include vhost/*.conf 在/usr/local/nginx/conf/下创建目录 #mkdir vhost/ //创 ...

  4. 11-4 12 Nginx安装 默认虚拟主机 用户认证 域名重定向

    2019独角兽企业重金招聘Python工程师标准>>> 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx域名重定向 扩展 ngin ...

  5. Nginx安装与虚拟主机配置shell脚本

    今天继续给大家介绍Linux运维相关知识,本文主要内容是Nginx安装与虚拟主机配置shell脚本. 一.NGINX安装 在今天的脚本上,我们开始尝试使用函数和参数,以增强脚本的灵活性和实现脚本的模块 ...

  6. Nginx——安装与虚拟主机配置(域名非80端口问题)

    一.引言 因为甲方提出问题:一个服务器与IP,两个域名a.b分别对应两个要部署的网站,这个一般是一个域名a对应一个主机服务器ip的默认80端口,另外的网站只能用其他端口(如b:8080对应主机ip:8 ...

  7. php编译安装, 编译安装nginx, yum安装nginx, nginx虚拟主机,默认虚拟主机

    编译安装PHP-7.3.0 1. 下载程序 下载php 7.3.0 cd /usr/local/src wget http://cn2.php.net/distributions/php-7.3.0. ...

  8. Apache用户认证、域名跳转、Apache访问日志

    httpd的用户认证 注意: 本章使用浏览器进行检测的前提是在物理机hosts文件添加虚拟机IP和虚拟主机域名. 配置用户认证 编辑httpd配置文件/usr/local/apache2.4/conf ...

  9. 12.6 Nginx安装 12.7 默认虚拟主机 12.8 Nginx用户认证 12.9 Nginx

    12.6 Nginx安装 [root@martin001 conf]# chkconfig --add nginx [root@martin001 conf]# chkconfig nginx on ...

最新文章

  1. autoware使用相机和深度学习进行目标检测(六)
  2. 2017年09月23日普级组 数列
  3. 华中科技大学应用高等工程数学_专业解析【第152期】| 机械电子工程课程设置及研究方向...
  4. react使用setState注意事项
  5. 服务器升级文件 不推送就无法打开吗,Win7升Windows10有获取通知,但是就不推送的解决方法...
  6. 腾讯广告算法大赛 | 初赛第一名心得分享
  7. Mybatis中出现java.sql.SQLException: 无效的列类型: 1111
  8. angularjs绑定属性_AngularJS隔离范围属性绑定教程
  9. Semaphore示例
  10. oracle----删除数据
  11. solr查询如何支持多个fq 多条件查询
  12. 给大家分享10个值得关注的C语言开源项目
  13. 【ArcGIS微课1000例】0019:什么是Shapefile文件?Shapefile文件之全解
  14. 公司企业邮箱开通使用,收不到邮件怎么办?
  15. 一个基于vue的时间轴轮播图插件
  16. lcd1602显示和led显示的区别在哪里
  17. mac 爱普生打印机驱动_爱普生l201打印机驱动下载Mac版-爱普生L201驱动Mac版下载 V8.7.5-PC6苹果网...
  18. 修改设置 打造安全的个人电脑(转)
  19. A4. MTK开机流程
  20. Minecraft PCL 启动器

热门文章

  1. 第十三周项目一-分数类中的运算符重载
  2. IOS开发笔记1-写一个hello world!程序
  3. Android之选项菜单创建
  4. 如何使用cmd进入打印机选项_怎样用命令行方式添加打印机端口? (已解决)
  5. python程序员面试宝典 勘误_《前端面试江湖》勘误合集(二)
  6. MySQL免安装版,遇到MSVCR120.dll文件丢失错误的解决方案
  7. LINUX 系统 安装Jexus 5.6和mono3.4 部署.net 环境
  8. 向安卓模拟器中添加文件
  9. J2EE(一)——开发简单WEB服务器
  10. sqlserver监控阻塞(死锁)具体情况