1.Nginx是一个高性能的 HTTP 和 反向代理 服务器,简介不多说官网上有烦死了(http://nginx.org/en)
2.安装nginx
请参以下网址
http://blog.youkuaiyun.com/qq_36030288/article/details/53858357
http://www.runoob.com/linux/nginx-install-setup.html
3.nginx的信号控制与进程管理
官方介绍更权威 (我介绍几个常用的命令)
http://nginx.org/en/docs/beginners_guide.html
stop - 快速关机
quit - 正常关机
reload - 重新加载配置文件
reopen - 重新打开日志文件
t - 检测配置是否正确
c - 自己选择配置文件
4.进入你安装nginx的目录中你会看到以下几个目录
....conf 配置文件
... html 网页文件
...logs 日志文件
...sbin 主要二进制程序
[root@man nginx]# ./sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
出现类似情况说明你的80端口被占用了
解决方法 更改端口或者杀掉80端口的程序
netstat -apn | grep 80
这样可以看出那个进程占用了端口号
直接kill -9 端口号 不好使话用 pkill -9 进程的名字
5.接下来看看nginx的配置文件
# 全局区
#user nobody;
worker_processes 1; # 有1个工作的子进程,可以自行修改,但太大无益,因为要争夺CPU,一般设置为 CPU数*核数
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#一般是配置nginx连接的特性 如1个word能同时允许多少连接
events {
worker_connections 1024; # 这是指 一个子进程最大允许连1024个连接
}
#Nginx默认是不允许列出整个目录的。
autoindex on; #展示目录
autoindex_exact_size on; # 显示文件的大小
autoindex_localtime on; #显示文件的时间
#这是配置http服务器的主要段
http {
include mime.types;
default_type application/octet-stream;
#这就是main格式 默认为main格式 也可自己写个格式都是无所谓的啊
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' #请求地址 用户 请求时间 所用的协议以及请求方法
'$status $body_bytes_sent "$http_referer" ' #状态码 发送了多少内容 请求来源
'"$http_user_agent" "$http_x_forwarded_for"'; #你用的客户端 (蜘蛛) 代理服务器的ip
#这就是自己自定义的个日志格式
log_format light '$remote_addr - $remote_user [$time_local] "$request" ';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;#这个跟http协议的版本有关
#gzip压缩功能设置
gzip on;
gzip_buffers 32 4k;
gzip_comp_level 6;
gzip_min_length 200;
gzip_types text/plain application/xml;
gzip_vary on;
#很重要的虚拟主机配置 一个server就好比一虚拟主机
server { # 这是虚拟主机段
listen 80;
server_name localhost;
#charset koi8-r;
#这里是采用日志的格式
access_log logs/host.access.log light;
location / {#定位,把特殊的路径或文件再次定位 ,把这个目录单独处理
root html; #根目录定位
index index.html index.htm; #默认页面
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#正则匹配与到以.php结尾的文件交给php进程进行处理
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#配置端口虚拟机
server {
listen 8000;
server_name a.com b.com ;配置多个域名
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/src/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
#其它虚拟主机,server 指令开始
}
4.nginx的日志备份+crontab
#!/bin/bash
base_path='/usr/local/src/nginx/logs'
log_path=$(date -d yesterday +"%Y%m")
day=$(date -d yesterday +"%d")
mkdir -p $base_path/$log_path
mv $base_path/access.log $base_path/$log_path/access_$day.log
kill -USR1 `cat /usr/local/src/nginx/logs/nginx.pid`
* * * * * 分 时 日 月 周 /to/path/b.sh 用上面写个sh脚本
5.location定位讲说 就是根据uri来定位的 (uri指的就是你域名或者ip后面的东西)
= 代表准确匹配
/ 代表一般匹配
~ 代表正则匹配
location [=|~|~*|^~] patt {
}
下面一个例子说明到底谁生效
location = / {
root /var/www/;
index index.htm index.html;
}
location / {
root /usr/local/src/nginx/html;
index index.html index.html;
}
如果直接访问ip什么也不加不就是uri吗
定位流程是
1 准确命中 然后 默认index index.htm
2.再次访问/index.htm 此次的uri是/index.htm了
3.最终结果为/usr/local/src/nginx/html/index.htm
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
location ~ image {
root /var/www/image;
index index.html;
}
正则会覆盖前面的效果最终以正则生效为准
location / {
root html;
index index.html index.htm;
}
location /man {
root /var/www/html;
index index.html index.htm index.php;
}
其实我们直接访问(http://www.xxx.com/man)都生效,但是最终以以最后一个为准,因为他匹配的长
6.rewrite重写
if (条件) {} 设定条件,再进行重写
set #设置变量
return #返回状态码
break #跳出rewrite
rewrite #重写
if 语法格式
if 空格 (条件) {
重写模式
}
"="来判断相等, 用于字符串比较
"~" 用正则来匹配(此处的正则区分大小写) ~* 不区分大小写的正则
-f -d -e来判断是否为文件,为目录,是否存在.
Nginx的学习
最新推荐文章于 2020-10-11 21:38:06 发布