linux下nginx安装
nginx的下载地址:
http://nginx.org/en/download.html,下载稳定版本,红色的是稳定版本
1、安装gcc环境
yum install gcc-c++
2、安装PCRE库,用于解析正则表达式
yum install -y pcre pcre-devel
3、zlib压缩和解压缩依赖,
yum install -y zlib zlib-devel
4、SSL安全的加密的套接字协议层,用于HTTP安全传输,也就是https
yum install -y openssl openssl-devel
5、mkdir /var/temp/nginx -p
tar -zxvf nginx-1.18.0.tar.gz
然后进入nginx目录
cd /usr/local/nginx-1.18.0
在目录下面执行
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid\
--Lock-path=/var/lock/nginx.lock\
--error-1og-path=/var/log/nginx/error.log\
--http-1og-path=/var/log/nginx/access.log\
--with-http_gzip_static_module\
--http-client-body-temp-path=/var/temp/nginx/client\
--http-proxy-temp-path=/var/temp/nginx/proxy\
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi\
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi\
--http-scgi-temp-path=/var/temp/nginx/scgi
7.在目录下面执行,make编译 make
8.在目录下面执行,安装 make install
9、cd /usr/local/nginx
然后执行./nginx
1、nginx 设置跨域支持
#允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
2、nginx日志分割
#!/bin/bash
LOG_PATH="/var/log/nginx/"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d+%H:%M)
PID=/var/run/nginx/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log
#向Nginx主进程发送信号,用于重新打开日志文件
kill -USR1 `cat $PID`
3、nginx防盗链
#对源站点验证
valid_referers *.imooc.com;
#非法引入会进入下方判断
if ($invalid_referer){
return 404;
}
4、nginx安装遇到的问题
出现这样的情况去conf下面的nginx.conf
新增 pid /usr/local/nginx/logs/nginx.pid;