安装依赖 1 yum -y install openssl* pcre*
openssl:如果在数据进行传输的过程中,没有加密认证,发送的数据很容易被抓包工具获取,容易暴露信息,openssl用于使用证书将传送的数据进行加密保证数据安全,通常要去网站配置ssl证书,实现https协议 pcre:为了支持rewrite,实现url的重定向,根据正则表达式进行内容跳转,跳转到指定的目标
解压源码包 1 tar zxf nginx-1.11.1.tar.gz
编译安装 1 2 cd /usr/src/nginx-1.11.1 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_spdy_module --with-http_stub_status_module --with-pcre && make && make install
–prefix=/usr/local/nginx 指定nginx的安装路径 –user=nginx 指定程序的用户 –group=nginx 指定程序的组用户 –with-http_ssl_module 支持https加密模块 –with-http_spdy_module 支持谷歌spdy,用于减小网络延迟,提高网速 –with-http_stub_status_module 支持nginx状态检查 –with-pcre 支持pcre重写功能
创建程序用户 1 useradd nginx -s /sbin/nologin
软链接 1 ln -s /usr/local/nginx/sbin/nginx /usr/sbin
配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 vi /usr/local/nginx/conf/nginx.conf 添加: #user nginx nginx; 配置文件含义: worker_processes 1; 开启nginx工作进程的数量 #error_log logs/error.log; #error_log logs/error.log notice; 错误日志的路径 #error_log logs/error.log info; #pid logs/nginx.pid; pid文件的路径 events { worker_connections 1024; 一个进程可以处理多少请求 } http { include 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"'; $remote_addr 和$http_x_forwaded_for记录客户端的ip地址 $remote_user 记录客户端用户的名称 $time_local:记录来访时间和时区 $request:记录请求的url和http协议 $status:记录请求状态(状态码) $body_bytes_sent:记录发送给客户端文件主体内容 $http_referer:用来记录从哪个页面链接进行跳转(访问)过来的 $http_user_agent:记录客户端浏览器的信息 keepalive_timeout 65; 链接超时时间 server { listen 80; 设置监听端口 server_name localhost; 配置域名信息,一个server对应一个域名 #charset koi8-r; #access_log logs/host.access.log main; location / { 表示跳转,当前写的location后面有/,表示如果在地址栏输入的地址为根目录的默认ip或者域名,直接将连接跳转到location中设置的页面上 root html; 设置网站的根目录,默认当前目录:为/usr/lcoal/nginx/html index index.html index.htm; 配置索引文件 }
启动服务 1 2 nginx netstat -lnpt | grep nginx