nginx-1.8.0反向代理服务器的搭建与配置

简介:
原创作品,允许转载,转载时请务必以超链接形式标明文章  原始出处 、作者信息和本声明。否则将追究法律责任。 http://dgd2010.blog.51cto.com/1539422/1684398

本文主要写CentOS和Ubuntu下nginx-1.8.0作为反向代理服务器时的一些编译安装的步骤和参数,将此nginx作为网站服务器用同样不成问题,文末有一些比较好的内核参数优化,有助于提高nginx的性能,可以作为nginx性能优化的一部分。

CentOS版(仅编译安装nginx)

1
2
3
4
5
6
7
8
9
10
11
12
13
yum  install  jemalloc jemalloc-devel -y    
groupadd -r www     
useradd  -r -g www www -c  "Web user"  -d  /dev/null  -s  /sbin/nologin     
wget -c http: //nginx .org /download/nginx-1 .8.0. tar .gz     
tar  zxf nginx-1.8.0. tar .gz     
cd  nginx-1.8.0     
. /configure  --user=www --group=www --prefix= /usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt= '-ljemalloc'     
make     
make  install     
/usr/local/nginx/sbin/nginx  -t     
/usr/local/nginx/sbin/nginx     
netstat  -ano |  grep  \:80     
ln  -s  /usr/local/nginx/sbin/nginx  /usr/bin/nginx

Ubuntu版(仅编译安装nginx,Ubuntu的www用户可以使用下面注释中的www-data)  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
apt-get  install  libjemalloc1 libjemalloc-dev   
apt-get  install  libpcre3 libpcre3-dev    
apt-get  install  openssl libssl1.0.0 libssl-dev    
groupadd -r www     
#www-data:x:33:    
useradd  -r -g www www -c  "Web user"  -d  /dev/null  -s  /sbin/nologin    
#www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin    
wget -c http: //nginx .org /download/nginx-1 .8.0. tar .gz    
tar  zxf nginx-1.8.0. tar .gz    
cd  nginx-1.8.0    
. /configure  --user=www --group=www --prefix= /usr/local/nginx  --with-http_stub_status_module --with-http_ssl_module --with-http_spdy_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-ld-opt= '-ljemalloc'    
make    
make  install    
/usr/local/nginx/sbin/nginx  -t    
ln  -s  /usr/local/nginx/sbin/nginx  /usr/bin/nginx    
ln  -s  /usr/local/nginx  /etc/nginx

自己用的CentOS版本(主要是安装目录、pcre、zlib、openssl)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
wget http: //nginx .org /download/nginx-1 .8.0. tar .gz 
tar  zxf nginx-1.8.0. tar .gz 
wget  ftp : //ftp .csx.cam.ac.uk /pub/software/programming/pcre/pcre2-10 .10. tar .gz 
wget  ftp : //ftp .csx.cam.ac.uk /pub/software/programming/pcre/pcre-8 .37. tar .gz 
mkdir  /usr/local/chrisdata 
tar  zxf pcre2-10.10. tar .gz 
tar  zxf pcre-8.37. tar .gz 
cd  pcre-8.37 
yum  install  gcc gcc-c++ -y 
. /configure  --prefix= /usr/local/chrisdata/pcre 
make 
make  install 
cd  .. 
cd  pcre2-10.10 
. /configure  --prefix= /usr/local/chrisdata/pcre2 
make 
make  install 
cd  .. 
wget http: //www .openssl.org /source/openssl-1 .0.2a. tar .gz 
tar  zxf openssl-1.0.2a. tar .gz 
wget http: //zlib .net /zlib-1 .2.8. tar .gz 
tar  zxf zlib-1.2.8. tar .gz 
cd  zlib-1.2.8 
ls 
. /configure  --prefix= /usr/local/chrisdata/zlib 
make 
make  install 
cd  .. 
cd  nginx-1.8.0 
. /configure  --prefix= /usr/local/chrisdata/nginx  --with-http_ssl_module --with-pcre= /root/pcre-8 .37 --with-zlib= /root/zlib-1 .2.8 
. /configure  --prefix= /usr/local/chrisdata/nginx  --with-http_ssl_module --with-openssl= /root/openssl-1 .0.2a --with-pcre= /root/pcre-8 .37 --with-zlib= /root/zlib-1 .2.8 --with-http_stub_status_module
. /configure  --prefix= /usr/local/chrisdata/nginx  --with-http_ssl_module --with-openssl= /root/openssl-1 .0.2a --with-pcre= /root/pcre-8 .37 --with-zlib= /root/zlib-1 .2.8
make 
make  install 
/usr/local/chrisdata/nginx/sbin/nginx  -t 
/usr/local/chrisdata/nginx/sbin/nginx 
netstat  -ano |  grep  \:80
ln  -s  /usr/local/chrisdata/nginx/sbin/nginx  /usr/bin/nginx
mkdir  /usr/local/nginx/conf/vhost

nginx主配置文件(lnmp一键安装包(lnmp1.2.tar.gz)、去掉注释)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
user www www;
worker_processes  auto;
error_log  logs /error .log crit;
pid        logs /nginx .pid;
 
events {
     use epoll;
     worker_connections  51200;
     multi_accept on;
}
 
http {
     include       mime.types;
     default_type  application /octet-stream ;
     sendfile        on;
     keepalive_timeout  65;
     tcp_nopush on;
     tcp_nodelay on;
     server_names_hash_bucket_size 128;
     client_header_buffer_size 32k;
     large_client_header_buffers 4 32k;
     client_max_body_size 50m;
     gzip  on;
     gzip_min_length  1k;
     gzip_buffers     4 16k;
     gzip_http_version 1.1;
     gzip_comp_level 2;
     gzip_types     text /plain  application /javascript  application /x-javascript  text /javascript  text /css  application /xml  application /xml +rss;
     gzip_vary on;
     gzip_proxied   expired no-cache no-store private auth;
     gzip_disable    "MSIE [1-6]\." ;
     server_tokens off;
     log_format  access   '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for' ;
     server {
         listen       80;
         server_name  localhost;
         location / {
             root   html;
             index  index.html index.htm;
         }
         error_page   500 502 503 504   /50x .html;
         location =  /50x .html {
             root   html;
         }
     }
 
     include vhost/*.conf;
}

proxy upstream日志格式设置(请求时间、请求长度、响应时间、响应状态、响应长度等)

1
2
3
4
5
6
log_format backendtest  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '$bytes_sent '
              '$request_length $request_time '
              '$upstream_response_time $upstream_status $upstream_response_length $upstream_header_time $upstream_addr '
              '"$http_user_agent" $http_x_forwarded_for' ;

upstream配置

1
2
3
4
upstream backendtest {
     server 192.168.1.102 weight=5;
     #...
}

proxy server配置(location后面的正则建议好好研究一下,如果启用stub_status,则必须为“/”,部分web程序由于写法问题可能有相对路径问题,如css,js,需要考虑做进一步的location配置)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
server {
     listen 8080;
     server_name localhost;
     #location ~ {
     location / {
         proxy_connect_timeout 300s;
         proxy_send_timeout 900;
         proxy_read_timeout 900;
         proxy_buffer_size 32k;
         proxy_buffers 4 32k;
         proxy_busy_buffers_size 64k;
         proxy_redirect off;
         proxy_hide_header Vary;
         proxy_set_header Accept-Encoding  '' ;
         proxy_set_header Host $host;
         proxy_set_header Referer $http_referer;
         proxy_set_header Cookie $http_cookie;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http: //backendtest ;
     }
     
     access_log  /home/wwwlogs/vhost-8080-backendtest-access .log backendtest;
 
     location  /nginx_status  {
             stub_status on;
             access_log   off;
         }
}

内核参数优化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Kernel sysctl configuration file for Red Hat Linux 
# 
# For binary values, 0 is disabled, 1 is enabled.  See sysctl(8) and 
# sysctl.conf(5) for more details. 
 
fs. file -max = 808127 
kernel.core_uses_pid = 1 
kernel.hung_task_timeout_secs = 0 
kernel.msgmax = 65536 
kernel.msgmnb = 65536 
kernel.sem = 250 32000 100 128 
kernel.shmall = 4294967296 
kernel.shmmax = 536870912 
kernel.shmmax = 68719476736 
kernel.shmmni = 4096 
kernel.sysrq = 0 
net.core.netdev_max_backlog = 262144 
net.core.rmem_default = 8388608 
net.core.rmem_max = 16777216 
net.core.somaxconn = 262144 
net.core.wmem_default = 8388608 
net.core.wmem_max = 16777216 
net.ipv4.conf.default.accept_source_route = 0 
net.ipv4.conf.default.rp_filter = 1 
net.ipv4.ip_forward = 0 
net.ipv4.ip_local_port_range = 1024 65535 
net.ipv4.tcp_fin_timeout = 1 
net.ipv4.tcp_keepalive_time = 1200 
net.ipv4.tcp_max_orphans = 3276800 
net.ipv4.tcp_max_syn_backlog = 262144 
net.ipv4.tcp_max_tw_buckets = 6000 
net.ipv4.tcp_mem = 94500000 915000000 927000000 
net.ipv4.tcp_rmem = 4096 87380 4194304 
net.ipv4.tcp_sack = 1 
net.ipv4.tcp_synack_retries = 5 
net.ipv4.tcp_syncookies = 1 
net.ipv4.tcp_syn_retries = 5 
net.ipv4.tcp_timestamps = 0 
net.ipv4.tcp_tw_recycle = 1 
net.ipv4.tcp_tw_reuse = 1 
net.ipv4.tcp_window_scaling = 1 
net.ipv4.tcp_wmem = 4096 16384 4194304

文件打开数优化

1
2
3
4
vim  /etc/security/limits .conf
 
* soft nofile 65535 
* hard nofile 65535

--end--

本文出自 “通信,我的最爱” 博客,请务必保留此出处http://dgd2010.blog.51cto.com/1539422/1684398

目录
相关文章
|
12月前
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
4063 1
|
12月前
|
弹性计算 定位技术 数据中心
阿里云服务器配置选择方法:付费类型、地域及CPU内存配置全解析
阿里云服务器怎么选?2025最新指南:就近选择地域,降低延迟;长期使用选包年包月,短期灵活选按量付费;企业选2核4G5M仅199元/年,个人选2核2G3M低至99元/年,高性价比爆款推荐,轻松上云。
1248 11
|
12月前
|
存储 弹性计算 网络协议
超详细的阿里云服务器购买流程,ECS自定义购买配置教程
本文详细图解阿里云ECS服务器自定义购买全流程,涵盖付费模式、地域选择、网络配置、实例规格、镜像、存储、安全组及登录设置等核心步骤,助您轻松掌握专业级云服务器搭建方法。
|
12月前
|
存储 弹性计算 监控
阿里云渠道商:如何挑选阿里云服务器配置?
本文详解通用型、计算型、内存型等实例适用场景,结合性能评估与成本优化策略,助力用户按需选择。以日均1万访问企业网站为例,2核4G+3M带宽月费约200元,性价比高。合理配置更省钱。
|
12月前
|
弹性计算
阿里云ECS云服务器8核16G配置收费价格,多种ECS实例CPU及费用清单
阿里云8核16G云服务器价格因实例类型而异。计算型c9i约743元/月,一年6450元(7折);通用算力型u1仅673元/月,一年4225元(5.1折)。实际价格享时长折扣,详情见ECS官网。
|
应用服务中间件 nginx
Nginx服务器的反向代理proxy_pass配置方法讲解
 Nginx服务器的反向代理proxy_pass配置方法讲解 这篇文章主要介绍了Nginx服务器的反向代理proxy_pass配置方法讲解,包括经常被提到的url的/问题的相关说明,需要的朋友可以参考下 就普...
6289 0
|
应用服务中间件 nginx
nginx服务器的反向代理proxy_pass配置方法
nginx服务器的反向代理proxy_pass配置方法
711 0