在linux下配置nginx服务器,话不多说,上步骤:
开发库
编译环境gcc g++ 开发库之类的需要提前装好。
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
pcre
安装PCRE pcre功能是让nginx有rewrite功能
下载PCRE:wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
解压安装包:tar zxvf pcre-8.35.tar.gz
进入安装包目录:cd pcre-8.35
编译:./configure
安装:make && make install
查看安装版本:pcre-config --version
如果出现版本号,说明安装成功
检查系统里是否安装了pcre软件
rpm -qa pcre
如果没有显示说明没有安装 反之安装过
rpm -e --nodeps pcre
删除pcre
下载nginx:wget http://nginx.org/download/nginx-1.6.2.tar.gz
解压安装包: tar zxvf nginx-1.6.2.tar.gz
进入安装包目录:cd nginx-1.6.2
编译安装:./configure
默认地址 /usr/local/nginx
安装:make
安装:make install
nginx配置:
cd /usr/local/nginx/conf ,把下面的内容覆盖到nginx.conf:
#运行nginx所在的用户名和用户组
#user www www;
#启动进程数
worker_processes 1;
#全局错误日志及PID文件
error_log /usr/local/nginx/logs/nginx_error.log crit;
#pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 65535;
#工作模式及连接数上限
events
{
use epoll;
worker_connections 65535;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http
{
#设定mime类型
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx/proxy.conf;
#charset gb2312;
#设定请求缓冲
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
#client_max_body_size 8m;
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 128k;
# gzip on;
# gzip_min_length 1k;
# gzip_buffers 4 16k;
# gzip_http_version 1.0;
# gzip_comp_level 2;
# gzip_types text/plain application/x-javascript text/css application/xml;
# gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
###禁止通过ip访问站点
#server{
# server_name _;
# return 404;
#}
upstream server_lb{
server localhost:8080 weight=2;#权重
server xxx.xxx.xxx.xxx:8080 weight=1;
}
server
{
listen 80;
server_name localhost;
#index index.html index.htm index.jsp;#设定访问的默认首页地址
#root /root/webapps/assets/;#设定网站的资源存放路径
#limit_conn crawler 20;
location / {
proxy_pass http://server_lb;
#通过下面这样设置使得nginx不托管静态资源
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#定义访问日志的写入格式
#log_format access '$remote_addr - $remote_user [$time_local] "$request" '
#'$status $body_bytes_sent "$http_referer" '
#'"$http_user_agent" $http_x_forwarded_for';
#access_log /usr/local/nginx/logs/localhost.log access;#设定访问日志的存放路径
}
}
可以检测配置的是否正确,如果出现syntax successful则说明你过成功。
/usr/local/nginx/sbin/nginx -t
启动nginx:/usr/local/nginx/sbin/nginx
启动的时候有报错,如下
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)...
这说明80接口有被占用,查看接口netstat -ntpl
kill -9 $pid
//杀掉进程
再次启动。
停止服务器
/usr/local/nginx/sbin/nginx -s stop
或 /usr/local/nginx/sbin/nginx -s quick
不停服更新conf:
/usr/local/nginx/sbin/nginx -s reload
正式安装之前,先执行nginx -V命令,结果如:
如果是如上图,说明http_stub_status_module已经安装好了,否则就执行以下步骤完成安装。
http_stub_status_module模块的源码nginx的自带的,只是nginx默认并没有安装这个模块。
重新下载tar.gz,然后执行下面的步骤:
执行configure命令:./configure --with-http_stub_status_module
make
cd /usr/local/nginx/sbin/
备份旧文件:mv nginx nginx.bak
将编译好的nginx执行文件拷贝到目标目录: cp /usr/local/nginx-1.10.1/objs/nginx /usr/local/nginx/sbin/
编辑配置文件,如下:vi /usr/local/nginx/conf/nginx.conf
location /ssm/nginx_status { #url不能和上面的location冲突
stub_status on;
access_log off;
}
重新加载nginx配置文件:nginx -s reload
访问下,如果结果是: curl 127.0.0.1/ssm/nginx_status
Active connections: 2
server accepts handled requests
7 7 249
Reading: 0 Writing: 1 Waiting: 1
就说明安装成功了,否则就需要kill -9杀死nginx进程,然后重启。