安装
更加安全的保障:
yum -y install gcc pcre-devel openssl-deve //安装所需的三个依赖包
useradd -s /sbin/nologin nginx
./configure //检测环境,检测所需依赖包是否安装好
make //编译,把代码编译成二进制
make install //安装
./configure
--prefix=/usr/local/nginx //指定安装路径 可指定可不指定。默认就是改目录下
--user=nginx
--group=nginx
--with-模块名 //在默认安装的模块上多安装指定模块
--with-http_ssl_module //开启SSL加密功能
安装时使用指定nginx用户安装。假如被攻击也只能获取nginx的权限,而不是超级管理员root的权限。
现代化软件,都是模块化设计
软件:100模块,功能
可以仅安装20个模块,
默认目录/usr/local/nginx/
conf 配置文件目录
html 存放网页目录
sbin 存放执行程序目录
logs 存放日志目录
/usr/local/nginx/sbin/nginx //开启服务
/usr/local/nginx/sbin/nginx -s stop //关闭服务
ln -s /usr/local/nginx/sbin/nginx /sbin/ //创建软链接,可直接使用nginx开启服务不需要打绝对路径
/usr/local/nginx/sbin/nginx -s reload //在nginx开启的状态后加载配置文件。不需要关服务即可做到。
netstat -antpul | grep nginx //查看nginx是否开启,查看监听端口
/usr/local/nginx/sbin/nginx -V //查看软件信息
vim /usr/local/nginx/conf/nginx.conf //全局配置【用户名,日志,进程】
.. ..
server {
listen 80; //基于ip,ip写在端口前即可 listen192.168.4.5:80;
server_name localhost;
auth_basic "xxx"; //认证提示符
auth_basic_user_file "/usr/local/nginx/pass"; //指定用户存放位置
location / { //位置放在nginx的/根目录下:/usr/local/nginx
root html; //省略了nginx默认路径:/usr/local/nginx 绝对路径为:/usr/local/nginx/html
index index.html index.htm;
}
}
make upgrade //升级
yum -y install httpd-tools
htpasswd -c /usr/local/nginx/pass tom //回车输入密码 -create 创建 后续创建用户不需要加 -c 再加-c会覆盖之前所有用户
cat /usr/local/nginx/pass
tom:$apr1$4Zg.RdLB$xEbpOsCfvf0Tfgq8rr6Fv.
虚拟主机(一台服务器可以实现多个网站)
基于域名,基于ip,基于端口
/etc/hosts 本地解析 优先级高于DNS ,首先询问/etc/hosts
server {
listen 80;
server_name www.b.com; //基于域名
charset utf-8; //网页显示支持中文以及其他语言;
location / {
root www; //存放在/usr/local/nginx/www/目录下
index index.html index.htm;
}
}
创建私钥
mkdir /etc/nginx/key
cd /etc/nginx/key
openssl genrsa -out server.key 1024
创建证书签名请求(Certificate Signing Request
openssl req -new -key server.key -out server.csr
回车之后会有一堆等着你输入的东西,其它东西我们都不需要关心,唯独一个Common Name要填成对应网站的IP或者域名,剩余的一路直接回车带过:
创建自签名证书
openssl x509 -req -in server.csr -signkey server.key -out server.crt
配置nginx
http {
...
server {
listen 80;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/key/server.crt;
ssl_certificate_key /etc/nginx/key/server.key;
...
}
}
隐藏nginx版本号
vim /usr/local/nginx/conf/nginx.conf
............
sendfile on;
#tcp_nopush on;
server_tokens off; //添加此行
#keepalive_timeout 0;
keepalive_timeout 65;
.....................
修改nginx服务名与版本号:
修改源码包里的这个文件:
src/core下的nginx.h文件
#define nginx_version 101010
#define NGINX_VERSION "7.88"
#define NGINX_VER "SSI/" NGINX_VERSION
源码编译软件设置开机自启 通常为了方便管理源码编译都安装在/usr/local/ 下
例如设置源码编译安装的nginx设置开机自启方法如下:
vim /etc/rc.local //此文件内的内容是开机就会执行的
.....
/usr/local/nginx/sbin/nginx //在尾行添加需要执行的命令,每次开机就会执行此命令,然后:wq保存并退出
/etc/rc.local ->/etc/rc.d/rc.local //但是/etc/rc.local是指向/etc/rc.d/rc.local这个文件的,而后者默认是没有执行权限的
chmod +x /etc/rc.d/rc.local //为/etc/rc.d/rc.local文件添加执行权限
wget https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7.rpm epel-release-latest-7.noarch.rpm
yum -y install yum-utils
yum-config-manager --enable remi-php71
yum -y install php php-opcache php-zip php-cli
yum -y install php-mysql php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-soap curl curl-devel php-redis php-event
yum -y install nginx nginx-mod-http-perl nginx-mod-stream nginx-filesystem nginx-mod-mail nginx-mod-http-image-filter nginx-all-modules nginx-mod-http-geoip nginx-mod-http-xslt-filter
yum install php-fpm -y
systemctl enable php-fpm
systemctl start php-fpm
systemctl enable nginx
systemctl start nginx
nginx访问ip限制
主配置文件引用白名单文件
geo $remote_addr $ip_whitelist {
default 0;
include ip_white.conf;
}
白名单文件写法
cat /etc/nginx/ip_white.conf
1.1.1.1 1;
2.2.2.2 1;
引用判断
if ($ip_whitelist = 1) {
break;
}
return 403;
直接判断
if ( $http_x_forwarded_for !~ 'ip1|ip2' ) {
return 403;
}
nginx日志分割
脚本内容:
#!/bin/bash
#Rotate the Nginx logs to prevent a single logfile from consuming too much disk space.
LOGS_PATH=/home/wwwlogs
YESTERDAY=$(date -d "yesterday" +%Y-%m-%d)
pid=/run/nginx.pid
mv ${LOGS_PATH}/access.log ${LOGS_PATH}/access-${YESTERDAY}.log
## 向 Nginx 主进程发送 USR1 信号。USR1 信号是重新打开日志文件
kill -USR1 $(cat $pid)
#定期清理日志,只保存3天的日志
find $LOGS_PATH -name "access-*.log" -type f -mtime +3 -exec rm {} \; > /dev/null 2>&1
计划任务:
0 0 * * * 脚本绝对路劲
nginx作为下载页
location /download {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
alias /data/liaotian-service;
}