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

12.6 Nginx安装

  1. cd /usr/local/src
  2. wget http://nginx.org/download/nginx-1.12.1.tar.gz
  3. tar zxf nginx-1.12.1.tar.gz
  4. ./configure --prefix=/usr/local/nginx //编译,根据需求,加上相应的参数模块,源码包尽量保留,有些模块在源码包里
  5. make && make install
  6. vim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx ) 启动脚本
  7. /usr/local/nginx/sbin/nginx -t /检查
  8. cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bak
  9. vim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)配置文件
    1. user 那个用户来启动服务
    2. worker_processes 定义子进程有几个
    3. worker_rlimit_nofile 最多访问多少个文件
    4. worker_conections 最大的连接数
    5. 60行,server 类似虚拟主机
  10. /usr/local/nginx/sbin/nginx -t
  11. /etc/init.d/nginx start
  12. ps aux |grep ngnix //ss父进程 S子进程
  13. curl localhost
  14. vi /usr/local/nginx/html/1.php //加入如下内容

<?php echo "test php scripts."; ?>

  1. curl localhost/1.php

12.7 默认虚拟主机

  1. vim /usr/local/nginx/conf/nginx.conf //删除server,http下增加 include vhost/*.conf
  2. mkdir /usr/local/nginx/conf/vhost
  3. cd vhost
  4. vim aaa.com.conf //加入如下内容 server { listen 80 default_server; // 有这个标记的就是默认虚拟主机 server_name aaa.com; index index.html index.htm index.php; root /data/wwwroot/default; }
  5. cd vhost
  6. mkdir /data/wwwroot
  7. vim index.html This is the fault index
  8. /usr/local/ngnix/sbin/ngnix -t
  9. /usr/local/ngnix/sbin/ngnix -s reload /重新加载
  10. curl -x137.0.0.1:80 aaa.com
  11. vhost目录下第一个为默认虚拟主机
  12. 加标志位default_server

12.8 Nginx用户认证

  1. 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 / //‘/’根目录名,可以换成其他的目录/admin或者匹配针对一个url- admin.php { auth_basic "Auth"; //定义用户的名字 auth_basic_user_file /usr/local/nginx/conf/htpasswd; 用户密码 } } 2. yum install -y httpd 3. htpasswd -c /usr/local/nginx/conf/htpasswd aming //生成密码 4. -t && -s reload //测试配置并重新加载 5. curl -uaming:lishiming -x127.0.0.1:80 test.com 6. mkdir /data/wwwroot/test.com 7. echo “test.com”>/data/wwwroot/test.com/index.html 8. curl -uaming:lishiming -x127.0.0.1:80 test.com

12.9 Nginx域名重定向

  1. 更改test.com.conf server { listen 80; server_name test.com test1.com test2.com; index index.html index.htm index.php; root /data/wwwroot/test.com; if ($host != 'test.com' ) { rewrite ^/(.*)$ http://test.com/$1 permanent; //定义主域名 } }
  2. server_name后面支持写多个域名,这里要和httpd的做一个对比
  3. 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

猜你喜欢

转载自my.oschina.net/u/3803446/blog/1826334