依赖解决
1. 更新yum源,可忽略
yum update
2. 安装gcc
nginx是C语言开发的,编译源码的时候需要使用gcc
yum install gcc-c++
3. 安装PCRE
一个Perl库, 包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
4. 安装zlib
nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
5. 安装openssl
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
编译安装
1. 下载nginx
wget http://nginx.org/download/nginx-1.8.0.tar.gz
2. 解压nginx
tar -zxvf nginx-1.8.0.tar.gz
3. 配置参数
cd nginx-1.8.0
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-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
4. make
make
5. install
install
Nginx使用
1. 启动
cd /usr/local/nginx/sbin
./nginx
2. 访问
访问:IP地址即可,如:http://192.168.1.102

3. 停止
3.1 快速停止
./nginx -s stop
3.2 完全停止
nginx任务处理完毕后停止
./nginx -s quit
3.3 kill进程
# 找到nginx对应的进程
ps aux | grep nginx
# kill -9 杀掉进程
kill -9 进程号
3.4 重启
重启可以先停掉nginx然后在运行一下即可重启,但是如果你只是修改一下nginx.conf
配置文件,可以使用如下命令:
./nginx -s reload
Nginx配置文件
1. 基本配置
下面配置文件中,配置了多个server
,都是监控80端口,mingyue-games.cn
域名指向nginx
默认的网页文件,zhuxinfeng.com
和zhuxinfeng.cn
两个域名在指向的http://127.0.0.1:8888
服务,同样监听的也都是80端口,也可以看出,多域名指向一个服务也可以使用空格分割,至于域名怎么解析就不解释了。
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name mingyue-games.cn;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name zhuxinfeng.com zhuxinfeng.cn;
location / {
proxy_pass http://127.0.0.1:8888;
}
}
}
2. 负载均衡
负载均衡很简单,使用不同的端口启用两个相同的服务,然后配置一下权重即可。
# weight是权重值,用户请求5次,平均访问8081端口3次,8082端口两次
upstream tomcat2 {
server 192.168.25.148:8081 weight=3;
server 192.168.25.148:8082 weight=2;
}
3. 配置选项
每个配置以分号结束
#配置用户或者组,默认为nobody nobody。
user administrator administrators;
#允许生成的进程数,默认为1
worker_processes 1;
#指定nginx进程运行文件存放地址
pid /nginx/pid/nginx.pid;
#指定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
error_log log/error.log debug;
events {
#设置网路连接序列化,防止惊群现象发生,默认为on
accept_mutex on;
#设置一个进程是否同时接受多个网络连接,默认为off
multi_accept on;
#事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
use epoll;
#最大连接数,默认为512
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型,默认为text/plain
default_type application/octet-stream;
#取消服务日志
access_log off;
#自定义日志格式
log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
#combined为日志格式的默认值
access_log log/access.log myFormat;
#允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
sendfile on;
#每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
sendfile_max_chunk 100k;
#连接超时时间,默认为75s,可以在http,server,location块。
keepalive_timeout 65;
# 负载均衡
upstream mysvr {
server 127.0.0.1:7878;
#热备
server 192.168.10.121:3333 backup;
}
#错误页
error_page 404 https://www.baidu.com;
server {
#单连接请求上限次数。
keepalive_requests 120;
#监听端口
listen 4545;
#监听地址
server_name 127.0.0.1;
#请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
location ~*^.+$ {
#根目录
root path;
#设置默认页
index vv.txt;
#请求转向mysvr 定义的服务器列表
proxy_pass http://mysvr;
#拒绝的ip
deny 127.0.0.1;
#允许的ip
allow 172.18.5.54;
}
}
}
该部分参考博文:Nginx配置详解