可去官网(http://nginx.org)下载至Windows,用rz命令上传php
[root@linux-10 ~]# cd /usr/local/src [root@linux-10 src]# rz
[root@linux-10 src]# tar -zxvf nginx-1.14.0.tar.gz
[root@linux-10 src]# cd nginx-1.14.0 [root@linux-10 nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
注:初始化时最好根据本身的需求增长相关模块的编译参数,这里因为课程须要暂不加参数。html
make && make install
一样支持-t选项linux
[root@linux-10 nginx-1.14.0]# ls /usr/local/nginx/sbin/ nginx [root@linux-10 nginx-1.14.0]# /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
vim /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/nginx
chkconfig --add nginx chkconfig nginx on
将原配置文件重命名,建立一个新的配置文件nginx
[root@linux-10 nginx-1.14.0]# cd /usr/local/nginx/conf [root@linux-10 conf]# mv nginx.conf nginx.conf.bak
vim nginx.conf //写入以下内容 (参考https://coding.net/u/aminglinux/p/aminglinux-book/git/blob/master/D15Z/nginx.conf)
user nobody nobody; //定义服务所属用户 worker_processes 2; //定义进程数量 error_log /usr/local/nginx/logs/nginx_error.log crit; //定义错误日志 pid /usr/local/nginx/logs/nginx.pid; //定义pid worker_rlimit_nofile 51200; //定义最大能够打开的文件数量 events { use epoll; worker_connections 6000; //定义最大链接数 }
注:上述仅是部分代码git
fastcgi_pass unix:/tmp/php-fcgi.sock;
server配置中的监听端口要与PHP中的配置文件保持一致vim
[root@linux-10 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@linux-10 conf]# /etc/init.d/nginx start Starting nginx (via systemctl): [ 肯定 ]
[root@linux-10 conf]# netstat -lntp |grep 80 tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 4066/nginx: master
在/usr/local/nginx/html/下编写测试文件1.phpcurl
vim 1.php <?php echo "test";
访问测试tcp
[root@linux-10 html]# curl localhost/1.php test
由于Nginx支持include语法,因此能够在配置文件中定义虚拟主机配置文件(相似于Apache)工具
删除原有配置文件中的server部分,增长包含虚拟主机配置文件的规则测试
mkdir /usr/local/nginx/conf/vhost
server { listen 80 default_server; // 有这个标记的就是默认虚拟主机 server_name lem.com; // 指定网站域名 index index.html index.htm index.php; //指定索引页 root /data/wwwroot/default; //指定网站存放位置 }
[root@linux-10 vhost]# mkdir -p /data/wwwroot/default/
vim index.html This is a default site.
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
注:从新加载相对于重启服务而言,它在配置文件有错误时,将不会从新加载,即不会破坏原有正在运行的Nginx服务。
[root@linux-10 conf]# curl localhost This is a default site. [root@linux-10 conf]# curl -x127.0.0.1:80 123.com This is a default site.
在vhost目录下,每个虚拟主机配置文件就是一个虚拟主机
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工具进行生成,若是本机存在Apache可直接使用,不然须要yum安装。
yum install -y httpd
生成密码文件
htpasswd -c /usr/local/nginx/conf/htpasswd lem
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
[root@linux-10 vhost]# curl -x 127.0.0.1:80 test.com -I HTTP/1.1 401 Unauthorized Server: nginx/1.14.0 Date: Fri, 08 Jun 2018 04:28:28 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive WWW-Authenticate: Basic realm="Auth"
location /admin/ //在配置文件中填写相应目录 { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
location ~ admin.php //location为定位语句,匹配指定内容 { auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/htpasswd; } }
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; } }
Nginx中支持识别多个域名,无需使用别名
permanent为永久重定向,状态码为301,若是写redirect则为302
ctrl+r :-t ctrl+r :-s
[root@linux-10 vhost]# curl -x 127.0.0.1:80 test2.com -I HTTP/1.1 301 Moved Permanently Server: nginx/1.14.0 Date: Fri, 08 Jun 2018 04:51:34 GMT Content-Type: text/html Content-Length: 185 Connection: keep-alive Location: http://test.com/
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