« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 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 | | | | | | | |
| 公告 |
戒除浮躁,读好书,交益友 |
Blog信息 |
blog名称:邢红瑞的blog 日志总数:523 评论数量:1142 留言数量:0 访问次数:9692713 建立时间:2004年12月20日 |

| |
[linux]Nginx+JSP 基础配置文档(转) 文章收藏, 网上资源, 软件技术, 电脑与网络
邢红瑞 发表于 2008/7/30 13:12:26 |
Nginx+JSP 基础配置文档(瞌睡原创) [可以代替apache+JSP]
测试环境:AS4U5+Nginx0.6.32+Tomcat5.5
1、安装pcre为了确保能在 Nginx 中使用正则表达式进行更灵活的配置,安装之前需要确定系统是否安装有 PCRE(Perl Compatible Regular Expressions)包,rpm包和tar.gz都可以。
rpm包:pcre-4.5-3.2.RHEL4和pcre-devel-4.5-3.2.RHEL4
tar.gz包:# wget ftp://ftp.csx.cam.ac.uk/pub/soft ... cre/pcre-7.7.tar.gz# tar zxvf pcre-7.7.tar.gz# cd pcre-7.7# ./configure# make# make install
2、安装nginx# wget http://sysoev.ru/nginx/nginx-0.6.32.tar.gz
# tar zxvf nginx-0.6.32.tar.gz# cd nginx-0.6.32# ./configure --with-http_stub_status_module –prefix=/usr/local/nginx# make# make install
注释:其中参数 --with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。安装成功后 /usr/local/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。其中 Nginx 的配置文件存放于 conf/nginx.conf,Nginx 只有一个程序文件位于 sbin 目录下的 nginx 文件。确保系统的 80 端口没被其他程序占用,运行 sbin/nginx 命令来启动 Nginx,打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
Nginx 安装后只有一个程序文件,本身并不提供各种管理程序,它是使用参数和系统信号机制对 Nginx 进程本身进行控制的。 Nginx 的参数包括有如下几个:-c <path_to_config>:使用指定的配置文件而不是 conf 目录下的 nginx.conf 。-t:测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。-v:显示 nginx 版本号。-V:显示 nginx 的版本号以及编译环境信息以及编译时的参数。
3、配置 Nginx
配置conf/nginx.conf#user nobody nobody; #工作进程的属主
worker_processes 2; # 工作进程数,一般与 CPU 核数等同
error_log /var/log/nginx.error_log;
pid logs/nginx.pid;
events { worker_connections 1024; # 每个工作进程允许最大的同时连接数 }
http { include mime.types; default_type application/octet-stream; include /usr/local/nginx/conf/proxy.conf; # 加载proxy.conf 也就是测试中用来链接JSP
access_log logs/access.log; # 日志文件名 sendfile on; keepalive_timeout 65; gzip on; gzip_comp_level 9;# include gzip.conf; # 集群中的所有后台服务器的配置信息# upstream tomcats { # server 192.168.0.11:8080 weight=10; # server 192.168.0.11:8081 weight=10; # server 192.168.0.12:8080 weight=10; # server 192.168.0.12:8081 weight=10; # server 192.168.0.13:8080 weight=10; # server 192.168.0.13:8081 weight=10; # }
server { listen 88; #http 端口 server_name localhost;
location /NginxStatus { stub_status on; #Nginx 状态监控配置 access_log off; }
location / { root /usr/local/test; #WEB工作主目录 index index.html index.htm index.jsp; }
# location ^/(WEB-INF)/ { # deny all; # }
#以扩展名方式匹配静态文件 location ~* \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { root /usr/local/test; expires 24h; }
#以目录方式匹配静态目录 location ~ ^/(images|pages|javascript|js|css|flash|media|static|tt)/ { root /usr/local/test; expires 30d; }
#以扩展名方式匹配动态文件 location ~* \.(jsp|do)$ { include /usr/local/nginx/conf/proxy.conf; proxy_pass http://localhost:8080; proxy_set_header X-Real-IP $remote_addr; }
#除了上面匹配过的内容全部转交Tomcat解析# location / {# include /usr/local/nginx/conf/proxy.conf;# proxy_pass http://localhost:8080;# proxy_set_header X-Real-IP $remote_addr;# }
# location / {# proxy_pass http://tomcats;# proxy_set_header X-Real-IP $remote_addr;# }
#error_page 404 /404.html; error_page 404 /50x.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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #}}
配置proxy.conf#!nginx (-)# proxy.confproxy_redirect off;proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;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_buffers 32 4k;
注释:Nginx 监控
上面是一个实际网站的配置实例,其中灰色文字为配置说明。上述配置中,首先我们定义了一个 location ~ ^/NginxStatus,这样通过 http://localhost/NginxStatus/ 就可以监控到 Nginx 的运行信息,显示的内容如下:
Active connections: 70 server accepts handled requests14553819 14553819 19239266 Reading: 0 Writing: 3 Waiting: 67 NginxStatus 显示的内容意思如下:
active connections – 当前 Nginx 正处理的活动连接数。 server accepts handled requests -- 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )。 reading -- nginx 读取到客户端的 Header 信息数。 writing -- nginx 返回给客户端的 Header 信息数。 waiting -- 开启 keep-alive 的情况下,这个值等于 active - (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
4、优化内核参数#ulimit -SHn 51200#vi /etc/sysctl.confnet.ipv4.tcp_fin_timeout = 30net.ipv4.tcp_keepalive_time = 300net.ipv4.tcp_syncookies = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.ip_local_port_range = 5000 65000#/sbin/sysctl -p
5、启动停止Nginx服务修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:#/usr/local/nginx/sbin/nginx -t
如果屏幕显示以下两行信息,说明配置文件正确: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully
启动命令#/usr/local/nginx/sbin/nginx
停止命令#killall nginx
修改配置文件后不停止nginx服务重启服务#kill -HUP `cat /usr/local/nginx/nginx.pid` |
|
|