- A+
所属分类:Nginx
1.安装编译工具及需要的环境 [root@localhost ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 2.安装PCRE(PCRE 作用是让 Ngnix 支持 Rewrite 功能,安装编译工具时系统会默认安装pcre 8.32,此处安装新版) [root@localhost ~]# cd /opt [root@localhost opt]# wget https://sourceforge.net/projects/pcre/files/pcre/8.39/pcre-8.39.tar.gz [root@localhost opt]# tar -zxvf pcre-8.39.tar.gz [root@localhost opt]# cd pcre-8.39 [root@localhost pcre-8.39]# ./configure --prefix=/usr/local/pcre [root@localhost pcre-8.39]# make [root@localhost pcre-8.39]# make install [root@localhost pcre-8.39]# pcre-config --version 查看pcre版本 如果./configure时不指定安装路径,执行pcre-config --version时会显示安装的版本。 此处./configure --prefix=/usr/local/pcre指定了安装路径,执行pcre-config --version会显示8.32,此时可以把8.39覆盖8.32 [root@localhost bin]# cp /usr/local/pcre/bin/pcre-config /usr/bin/pcre-config 再执行 pcre-config --version就会显示8.39 3.下载稳定版源码包并解压 [root@localhost opt]# wget http://nginx.org/download/nginx-1.13.0.tar.gz [root@localhost opt]# tar zxvf nginx-1.13.0.tar.gz [root@localhost opt]# cd nginx-1.13.0 [root@localhost nginx-1.13.0]# ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.39 此处pcre配置路径为pcre的源码包 3.安装nginx [root@localhost nginx-1.13.0]# make [root@localhost nginx-1.13.0]# make install 4.查看版本 [root@localhost nginx-1.13.0]# /opt/nginx/sbin/nginx -v nginx version: nginx/1.13.0 5.配置用户 [root@localhost nginx-1.13.0]# groupadd www [root@localhost nginx-1.13.0]# useradd -g www www 6.配置nginx.conf [root@localhost nginx-1.13.0]# vim /opt/nginx/conf/nginx.conf user www www; worker_processes 2; #设置值和CPU核心数一致 error_log /opt/nginx/logs/nginx_error.log crit; #日志位置和日志级别,crit 记录的日志最少,而debug记录的日志最多 pid /opt/nginx/nginx.pid; #指定进程id的存储文件位置 events #设定nginx工作模式及连接数上限 { use epoll; #指定工作模式,select和poll是标准模式,kqueue和epoll是高效模式,kqueue用在bsd系统,linux系统epol是首选 worker_connections 65535; #定义每个进程的最大连接数,默认是1024 multi_accept on; #获得有新连接的通知之后,接受尽可能多的连接,需要注意的是:如果worker_connections设置太低的话,这样可能会造成拥堵 } http {
#log_format main '$remote_addr - $remote_user [$time_local] "$request" ' $remote_add与$http_x_forwarded_for用于记录客户端ip # '$status $body_bytes_sent $remote_user用来记录客户端用户名称 # "$http_referer" ' $http_user_agent:记录客户浏览器的相关信息; # '"$http_user_agent" "$http_x_forwarded_for"';
include mime.types;
default_type application/octet-stream; #默认类型为二进制流
server_names_hash_bucket_size 128;
client_header_buffer_size 32k; #指定来自客户端请求头的headerbuffer大小
large_client_header_buffers 4 32k; #指定客户端请求中较大的消息头的缓存最大数量和大小
client_max_body_size 50m; #设置允许客户端请求的最大单个文件字节数
sendfile on; #开启高效文件传输模式
tcp_nopush on; #防止网络堵塞
keepalive_timeout 60; #客户端连接保持活动的超时时间
tcp_nodelay on; #防止网络堵塞
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
#httpgzip模块,支持在线实时压缩输出数据流
gzip on; #开启gzip
gzip_min_length 1k; #允许压缩的页面最小字节数
gzip_buffers 4 16k; #申请4个单位为16k的内存作为压缩结果流缓存
gzip_http_version 1.1; #识别http协议,默认1.1
gzip_comp_level 2; #指定gzip压缩比
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss; #指定压缩的类型
gzip_vary on; #让前端的缓存服务器缓存经过gzip压缩的页面
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
#limit_conn_zone $binary_remote_addr zone=perip:10m;
server_tokens off;
access_log off;
server {
listen 80; #监听端口
server_name localhost; #指定ip或域名
index index.html index.htm index.php; #默认首页地址
root /opt/nginx/html; #网页根目录
以下静态文件交给nginx处理,expires指定静态文件过期时间为30天
location - .*\.(gif|jpg|jpeg|png|bmp|swf)${
root /opt/nginx/html;
expires 30d;
}
把upload和html下的所有文件交给nginx处理
location - ^/(upload|html)/{ root /opt/nginx/html; expires 30d;
}
所有.jsp文件都交给本机的8080端口处理,可配置tomcat
location ~ .*.jsp${ index index.jsp; proxy_pass http://localhost:8080; }
#默认nginx不支持php拓展,如需要,修改此处
location ~ \.php
7.验证nginx配置
[root@localhost nginx-1.13.0]# /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
8.启动服务
/opt/nginx/sbin/nginx
/opt/nginx/sbin/nginx -s reload #重新载入配置文件
/opt/nginx/sbin/nginx -s reopen #重启nginx
/opt/nginx/sbin/nginx -s stop #停止nginx