相关地址
相关知识点
安装
yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim
查看Nginx的安装目录
rpm -ql nginx
nginx.conf文件解读
nginx.conf 文件是Nginx总配置文件,在我们搭建服务器时经常调整的文件。
进入etc/nginx目录下,然后用vim进行打开
cd /etc/nginx
vim nginx.conf
下面是文件的详细注释
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/mime.types; #文件扩展名与类型映射表
default_type application/octet-stream; #默认文件类型
#设置日志模式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
default.conf 配置项
进入conf.d目录,然后使用vim default.conf进行查看。
server {
listen 80; #配置监听端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/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
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
}
Nginx的启动、停止和重启
启动
在CentOS7.4版本里(低版本是不行的),是可以直接直接使用nginx启动服务的。
nginx
使用systemctl命令启动
systemctl start nginx.service
查看nginx是否启动
ps aux | grep nginx
停止
停止Nginx服务的四种方法
- 立即停止服务:
nginx -s stop - 从容停止服务:
nginx -s quit - killall 方法杀死进程:
killall nginx - systemctl 停止:
systemctl stop nginx.service
重启
重启Nginx服务:systemctl restart nginx.service
重新载入配置文件:nginx -s reload
查看端口号占用情况:netstat -tlnp
Nginx访问权限
deny是禁止访问,allow是允许访问。下面的配置只允许45.76.202.231进行访问,其他的IP是禁止访问的
location / {
allow 45.76.202.231;
deny all;
}
对于网站下的img(图片目录)是运行所有用户访问,但对于网站下的admin目录则只允许公司内部固定IP访问,=号代表精确匹配
location =/img{
allow all;
}
location =/admin{
deny all;
}
使用正则表达式设置访问权限
以下设置不能访问以php结尾的文件了
location ~\.php$ {
deny all;
}
反向代理
最简单的反向代理
现在我们要访问http://nginx2.jspang.com然后反向代理到http://jspang.com这个网站。我们直接到etc/nginx/con.d/8001.conf进行修改。
修改后的配置文件如下:
server{
listen 80;
server_name nginx2.jspang.com;
location / {
proxy_pass http://jspang.com;
}
}
一般我们反向代理的都是一个IP,但是我这里代理了一个域名也是可以的。
在浏览器中打开http://nginx2.jspang.com测试。
其它反向代理指令
反向代理还有些常用的指令
proxy_set_header:在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。proxy_connect_timeout:配置Nginx与后端代理服务器尝试建立连接的超时时间。proxy_read_timeout: 配置Nginx向后端服务器组发出read请求后,等待相应的超时时间。proxy_send_timeout:配置Nginx向后端服务器组发出write请求后,等待相应的超时时间。proxy_redirect:用于修改后端服务器返回的响应头中的Location和Refresh。
Nginx适配PC端和移动端
Nginx通过内置变量$http_user_agent,可以获取到请求客户端的userAgent,就可以用户目前处于移动端还是PC端,进而展示不同的页面给用户。
在/usr/share/nginx/目录下新建两个文件夹,分别为:pc和mobile目录
cd /usr/share/nginx
mkdir pc
mkdir mobile
在pc和miblic目录下,新建两个index.html文件,文件里下面内容
<h1>I am pc!</h1>
<h1>I am mobile!</h1>
进入etc/nginx/conf.d目录下,修改8001.conf文件,改为下面的形式:
server{
listen 80;
server_name nginx2.jspang.com;
location / {
root /usr/share/nginx/pc;
if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
root /usr/share/nginx/mobile;
}
index index.html;
}
}
Nginx配置Gzip
gzip最简单的配置
http {
.....
gzip on;
gzip_types text/plain application/javascript text/css;
.....
}