本文共 2274 字,大约阅读时间需要 7 分钟。
12.6 Nginx安装
12.7 默认虚拟主机12.8 Nginx用户认证12.9 Nginx域名重定向扩展
nginx.conf 配置详解 nginx rewrite四种flagcd /usr/local/src
wget tar zxf nginx-1.12.1.tar.gzcd /usr/local/src/nginx-1.12.1./configure --prefix=/usr/local/nginxmake && make installvim /etc/init.d/nginx //复制如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/etc_init.d_nginx )chmod 755 /etc/init.d/nginxchkconfig --add nginx chkconfig nginx on cd /usr/local/nginx/conf/; mv nginx.conf nginx.conf.bakvim nginx.conf //写入如下内容(参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)/usr/local/nginx/sbin/nginx -t/etc/init.d/nginx startnetstat -lntp |grep 80 测试php解析vi /usr/local/nginx/html/1.php //加入如下内容<?phpecho "test php scripts.";?>curl localhost/1.phpvim /usr/local/nginx/conf/nginx.conf //增加
include vhost/*.confmkdir /usr/local/nginx/conf/vhostcd !$; vim default.conf //加入如下内容server{ listen 80 default_server; // 有这个标记的就是默认虚拟主机server_name aaa.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 reloadcurl localhostcurl -x127.0.0.1:80 123.comvim /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;}}yum install -y httpdhtpasswd -c /usr/local/nginx/conf/htpasswd aming-t && -s reload //测试配置并重新加载mkdir /data/wwwroot/test.comecho “test.com”>/data/wwwroot/test.com/index.htmlcurl -x127.0.0.1:80 test.com -I//状态码为401说明需要验证curl -uaming:passwd 访问状态码变为200编辑windows的hosts文件,然后在浏览器中访问test.com会有输入用户、密码的弹窗
针对目录的用户认证location /admin/{ auth_basic "Auth";auth_basic_user_file /usr/local/nginx/conf/htpasswd;}更改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 ^/(.*)$ permanent;}}server_name后面支持写多个域名,这里要和httpd的做一个对比permanent为永久重定向,状态码为301,如果写redirect则为302转载于:https://blog.51cto.com/13583139/2107474