【nginx】Nginx的编译安装和配置介绍

1.首先安装一些依赖包
yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
gcc:编译器
openssl:用于网站加密通讯
oppenssl-devel:开发软件包
pcre:用于支持解析正则表达式
zlib:用于对数据进行解压缩。网站之间通信时,数据先压缩再传输,节省网络带宽

2.下载nginx安装包
http://nginx.org/en/download.html  //下载稳定版nginx-1.14.2.tar.gz

3.解压到如下目录
cd /htdocs/software
tar zxf nginx-1.14.2.tar.gz

4.设置编译方案编译(目录位置:cd /htdocs/software/nginx-1.14.2)
./configure --prefix=/data/nginx --user=www --group=www --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_addition_module 
--with-http_image_filter_module --with-http_gzip_static_module --with-pcre
其中:
--prefix=/data/nginx  //安装目录
--user=www    //设置nginx工作进程的用户
--with-http_ssl_module   //附加了 http_ssl 模块用于支持 Https

5.编译和安装
make && make install

6.校验Nginx配置文件是否正确
/data/nginx/sbin/nginx -t  //编译安装时的目录--prefix=/data/nginx
./sbin/nginx -V   // 可以看到编译选项

7.启动重启关闭nginx
/data/nginx/sbin/nginx   //启动
/data/nginx/sbin/nginx -s reload   //重启
/data/nginx/sbin/nginx -s reload   //关闭

8.Nginx配置(nginx.conf)
sever部分:用于指定虚拟主机域名、IP和端口
location部分:用于匹配网页位置,location继承server,一般写在sever里
这边有两种写法,一个是location直接写在sever里,另一种是location新起一个文件,然后在sever用include包含这个location文件,如
include /data/nginx/conf/location_params

sever和location具体配置方法 ,如下

nginx.conf中的sever部分:
server
{
        listen 7300;
        server_name xxx.xx.xx.xxx;
        root /data/www/s2b/supplier/dist_supplier;
        index index.html;
        include /data/nginx/conf/location_params;
}
root /data/www/s2b/supplier/dist_supplier:站点根目录,你网站文件存放的地方
index: 请求指向index.html页面,这个index.html需要在dist_supplier目录下
nginx.conf同目录下存在location_params文件
location /
{
        add_header Content-Security-Policy upgrade-insecure-requests;
        try_files $uri $uri/ @rewrites;
}
location @rewrites {
        rewrite ^(.+)$ /index.html last;
}
简介:
1.add_header:用于设置response header
2.Content Security Policy:内容安全策略,简称CSP。是由W3C小组定义的一项规范,其主要作用是提供一个额外的安全层,用于检测并削弱某些特定类型的攻击,包括跨站脚本 (XSS) 和数据注入攻击等
3.upgrade-insecure-requests:使http请求自动过渡成https请求,使其不会报错
4.try_files $uri $uri/ @rewrites :
 4.1.$uri :这个是nginx的一个变量,存放着用户访问的地址
           比如:http://www.xxx.com/index.html, 那么$uri就是 /index.html
 4.2.$uri/ :代表访问的是一个目录
           比如:http://www.xxx.com/hello/test/,那么$uri/就是 /hello/test/
 4.3.try_files: 用户访问一个网址,nginx去尝试到网站目录读取用户访问的文件,如果第一个变量存在,就直接返回;不存在继续读取第二个变量,如果存在,直接返回;不存在直接跳转到第三个参数上
 参考文献:https://www.cnblogs.com/bigberg/p/7644192.html

参考文献:
https://www.cnblogs.com/bluestorm/p/4574688.html
https://www.cnblogs.com/wcwnina/p/8728391.html
https://www.cnblogs.com/jiangxiaobo/p/9856319.html !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章