一、安装
1、下载(此处不再赘述,忽略)
2、解压到指定目录
[root@localhost home]# tar zxvf nginx-1.11.2.tar.gz -C /myapp/
[root@localhost home]# cd /myapp/nginx-1.11.2/
[root@localhost nginx-1.11.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
3、使用yum安装依赖库 【gcc 、pcre、openssl、zilib】
[root@localhost nginx-1.11.2]# yum -y install gcc
[root@localhost nginx-1.11.2]# yum -y install pcre
[root@localhost nginx-1.11.2]# yum -y install openssl
[root@localhost nginx-1.11.2]# yum -y install zlib
4、编译并安装
[root@localhost nginx-1.11.2]# ./configure –prefix=/usr/local/nginx –with-http_stub_status_module
[root@localhost nginx-1.11.2]# make
[root@localhost nginx-1.11.2]# make install
5、说明:
参数 –with-http_stub_status_modul是为了启用nginx的 NginxStatus 功能,用来监控 Nginx 的当前状态
–prefix=/usr/local/nginx 指定安装目录更详细的参数参考./configure –help
安装成功后 /usr/local/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。
二、配置Nginx
1、配置
#user nobody;
user root root; #工作进程的属主
worker_processes 1; #工作进程数,一般与 CPU核数等同
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
############################################################################
# 使用的网络I/O模型,Linux系统推荐采用epoll模型,FreeeBSD系统推荐采用kqueue模型use epoll;
############################################################################
worker_connections 1024; #每个工作进程允许最大的同时连接数
}
http {
include mime.types;
default_type application/octet-stream;
############################################################################
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
# 设置客户端能够上传的文件大小,注意要与应用程序中的文件大小限制兼容。
client_max_body_size 10m;
# sendfile on;
tcp_nopush on;
# keepalive_timeout 60;
tcp_nodelay on;
client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;
# 以下放开注释 自定义日志记录格式设置,main为名字,在access_log命令中引用
log_format main '$remote_addr – $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
#指定日志存放路径,如果想使用默认的combined格式记录日志,可以使用access_log logs/access.log combined; 以下是使用log_format自定义的格式记录日志的。
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
############################################################################
# 开启gzip压缩设置(只能在http模块中设置)
gzip on; #放开注释
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types application/x-javascript text/css application/xml;
gzip_vary on;
#upstream设置,设置代理服务器(负载均衡池),默认的负载均衡方式是轮询,另外一种是ip_hash
upstream tomcat_server{
ip_hash; #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
server 10.10.10.145:8081 weight=10;
server 10.10.10.146:8082 weight=10;
}
#处理上传和下载的图片文件服务器,设置代理服务器(负载均衡池),默认的负载均衡方式是轮训,另外一种是ip_hash
upstream image_server{
server 10.10.10.148:8083 weight=10;
}
############################################################################
server {
listen 8090; #nginx侦听端口 根据自己需要修改 我的8080端口已经被占用,所以修改为8090
server_name localhost;
############################################################################
charset utf-8;
index index.html index.htm index.jsp index.do;
############################################################################
#charset koi8-r;
############################################################################
#rewrite settings
if (-d $request_filename)
{
rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
}
############################################################################
#access_log logs/host.access.log main;
location / {
#root html;
index index.html index.htm index.jsp index.do;
}
############################################################################
# iamge 服务器location
location ~*/admin/images/{
# alias /web/www/html/img/;
proxy_pass http://image_server;
proxy_set_header Host $host:8083;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~*/admin/
{
# 如果后端服务器返回502,504,执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现failover。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_pass http://tomcat_server;
proxy_redirect off;
# 变量$host等于客户端请求头中的Host值
#proxy_set_header Host $host:8081;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
#后端的web服务器可以通过X-Forwarded-For获取真实的IP地址,$remote_addr客户端的ip地址
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#image expires settings
# expires 属于http Header模块,主要用来Nginx返回给用户网页添加附件的header信息,可以在http,server,location中使用
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
#css&js expires settings
# expires 属于http Header模块,主要用来Nginx返回给用户网页添加附件的header信息,可以在http,server,location中使用
location ~ .*.(js|css|html)$
{
expires 2h;
}
# 放开注释
error_page 404 /404.html;
############################################################################
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2、测试
测试配置文件是否正确
[root@localhost conf]# /usr/local/nginx/sbin/nginx -t
如果显示以下信息,说明配置正确可以启动Nginx服务
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3、启动、重启、停止、重新加载配置文件命令
启 动nginx: /usr/local/nginx/sbin/nginx
重 启Nginx: /usr/local/nginx/sbin/nginx -s reload
停 止Nginx:/usr/local/webserver/nginx/sbin/nginx -s stop
重新加载配置文件:/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf