篇八:Linux搭建nginx服务
导语:nginx代理服务器,目前作者主要可用于两方面,一方面是代理tomcat服务,搭建多台tomcat服务器,然后通过nginx负载均衡,解决高访问量为题;另一方面用于代理静态文件,可用作文件服务器,上传文件后借用nginx暴露到网络中,可供其它用户访问。
一、nginx安装
a、下面的nginx安装,实现了基本的nginx的安装yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
cd /usr/local
tar -zxvf nginx-1.6.2.tar.gz
mkdir nginx
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx
make && make install
vi /etc/sysconfig/iptables
>> -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart
b、nginx安装,包含ssl的支持
yum install gcc gcc-c++ make automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel
tar -zxvf nginx-1.6.2.tar.gz
mkdir nginx
cd nginx-1.6.2
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make && make install
vi /etc/sysconfig/iptables
>> -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
# service iptables restart
c、基本操作
检查配置文件:/usr/nginx/sbin/nginx -t
平滑重启:/usr/nginx/sbin/nginx -s reload
关闭:ps -ef|grep nginx 查看进程
kill -9 进程
启动:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
二、nginx+tomcat负载均衡
a、配置点分析
1、核心配置,在upstream中配置负载的tomcat的IP和端口,也可以通过"server 192.168.2.149:8080 weight=1"配置权重,值越大,权重越大;
同一个web,部署多个tomcat,每一个对应的session不一致,加上"ip_hash",同一个ip固定访问一台服务
upstream hostname {
ip_hash;
server 192.168.2.149:8080;
server 192.168.1.9:8080;
}
2、在server中启用负载均衡,通过hostname绑定
location / {
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}
b、http与https的配置区别:每个初始化的nginx.conf都有两个server,分别对应http和https,不同的server配置不同的端口,通过端口控制http或者https
#HTTP
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 8080 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx/SSL/2_202.104.122.66.crt;
ssl_certificate_key /usr/local/nginx/SSL/3_202.104.122.66.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;
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}
}
c、参考demo1
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
upstream hostname{
server 192.168.1.70:8081 weight=1;
server 192.168.1.108:8080 weight=10;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 8080 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx/SSL/2_202.104.122.66.crt;
ssl_certificate_key /usr/local/nginx/SSL/3_202.104.122.66.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;
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}
}
}
d、参考demo2
user www www;
worker_processes 2;
#error_log logs/error.log info;
#pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
#单个进程允许的最大连接数
worker_connections 65535;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
include mime.types;
#默认文件类型
default_type application/octet-stream;
#服务器名字的hash表大小
server_names_hash_bucket_size 128;
#客户端请求头缓冲大小。nginx默认会用client_header_buffer_size这个buffer来读取header值,
#如果header过大,它会使用large_client_header_buffers来读取。
#如果设置过小HTTP头/Cookie过大 会报400 错误 nginx 400 bad request
#如果超过buffer,就会报HTTP 414错误(URI Too Long)
#nginx接受最长的HTTP头部大小必须比其中一个buffer大,否则就会报400的HTTP错误(Bad Request)。
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
#客户端请求体的大小
client_body_buffer_size 8m;
#隐藏ngnix版本号
server_tokens off;
ignore_invalid_headers on;
#指定启用除第一条error_page指令以外其他的error_page。
recursive_error_pages on;
#让 nginx 在处理自己内部重定向时不默认使用 server_name 设置中的第一个域名
server_name_in_redirect off;
#开启文件传输,一般应用都应设置为on;若是有下载的应用,则可以设置成off来平衡网络I/O和磁盘的I/O来降低系统负载
sendfile 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; #压缩类型
#upstream作负载均衡,在此配置需要轮询的服务器地址和端口号,max_fails为允许请求失败的次数,默认为1.
upstream hostname {
server 192.168.2.149:8080;
server 192.168.1.9:8080;
}
#主机配置
server {
#监听端口
listen 80;
server_name hostname;
#字符集
charset utf-8;
#单独的access_log文件
access_log logs/192.168.2.149.access.log main;
#反向代理配置,将所有请求为http://hostname的请求全部转发到upstream中定义的目标服务器中。
location / {
proxy_pass http://hostname;
proxy_set_header X-Real-IP $remote_addr;
}
#启用nginx status 监听页面
#location /nginxstatus {
# stub_status on;
# access_log on;
#}
#错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
三、nginx做文件服务器
参考篇九--vsftpd文件服务器

nginx上传最大值问题:


浙公网安备 33010602011771号