将nginx配置成url_hash转发方式

所需软件名称及版本:
pcre-8.02.tar.gz
nginx-0.7.58.tar.gz
Nginx_upstream_hash-0.3.1.tar.gz

 

1、安装pcre

Java代码
  1. tar -zxvf pcre-8.02.tar.gz
  2. cd pcre-8.02/
  3. ./configure
  4. make && make install
tar -zxvf pcre-8.02.tar.gz cd pcre-8.02/ ./configure make && make install 

 

2、给nginx安装url_hash补丁

Java代码
  1. tar -zxvf nginx-0.7.58.tar.gz
  2. tar -zxvf Nginx_upstream_hash-0.3.1.tar.gz
  3. cd nginx-0.7.58/
  4. patch -p0 < /var/tmp/loojoy/nginx_upstream_hash-0.3.1/nginx.patch
tar -zxvf nginx-0.7.58.tar.gz tar -zxvf Nginx_upstream_hash-0.3.1.tar.gz cd nginx-0.7.58/ patch -p0 < /var/tmp/loojoy/nginx_upstream_hash-0.3.1/nginx.patch

 

3、安装nginx

Java代码
  1. groupadd www
  2. useradd www -g www
  3. ./configure --prefix=/usr/local/webserver/nginx \
  4. --user=www \
  5. --group=www \
  6. --add-module=/var/tmp/loojoy/nginx_upstream_hash-0.3.1 \
  7. --with-http_ssl_module \
  8. --with-http_stub_status_module
groupadd www useradd www -g www ./configure --prefix=/usr/local/webserver/nginx \ --user=www \ --group=www \ --add-module=/var/tmp/loojoy/nginx_upstream_hash-0.3.1 \ --with-http_ssl_module \ --with-http_stub_status_module 

 

4、检查nginx生成的配置文件是否正确

Java代码
  1. /usr/local/webserver/nginx/sbin/nginx -t
 /usr/local/webserver/nginx/sbin/nginx -t 

5、启动nginx
若nginx.conf检查通过,则启动nginx。

Java代码
  1. /usr/local/webserver/nginx/sbin/nginx
/usr/local/webserver/nginx/sbin/nginx 

 

6、查看端口80
netstat -ant

7、通过浏览器访问:
http://your_nginx_server_ip/
若可以正常访问,则nginx安装成功。

8、修改nginx的配置文件如下:

Java代码
  1. -------------------[S]url_hash转发方式nginx.conf[S]--------------------
  2. user www www;
  3. worker_processes 10;
  4. error_log logs/error.log;
  5. #error_log logs/error.log notice;
  6. #error_log logs/error.log info;
  7. #pid logs/nginx.pid;
  8. worker_rlimit_nofile 51200;
  9. events
  10. {
  11. use epoll;
  12. worker_connections 51200;
  13. }
  14.  
  15. http
  16. {
  17. include mime.types;
  18. default_type application/octet-stream;
  19. keepalive_timeout 120;
  20. tcp_nodelay on;
  21.  
  22. upstream your.website.name{
  23. server your.tomcat.server.ip1:8080 ;
  24. server your.tomcat.server.ip1:8080 ;
  25.  
  26. hash $request_uri;
  27. }
  28.  
  29. server
  30. {
  31. listen 80;
  32. server_name your.website.name;
  33.  
  34. location / {
  35. proxy_pass http://your.website.name;
  36. proxy_set_header Host $host;
  37. proxy_set_header X-Real-IP $remote_addr;
  38. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  39. }
  40.  
  41. log_format www_gjw_com '$remote_addr - $remote_user [$time_local] $request '
  42. '"$status" $body_bytes_sent "$http_referer" '
  43. '"$http_user_agent" "$http_x_forwarded_for"';
  44. #access_log /data1/logs/www.log www_gjw_com;
  45.  
  46. location /nginx_status {
  47. stub_status on;
  48. access_log off;
  49. }
  50. }
  51. }
  52.  
  53. -------------------[E]url_hash转发方式nginx.conf[E]--------------------
-------------------[S]url_hash转发方式nginx.conf[S]-------------------- user www www; worker_processes 10; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid        logs/nginx.pid; worker_rlimit_nofile 51200;          events {       use epoll;       worker_connections 51200; }  http {       include       mime.types;       default_type application/octet-stream;       keepalive_timeout 120;       tcp_nodelay on;        upstream your.website.name{               server   your.tomcat.server.ip1:8080 ;               server   your.tomcat.server.ip1:8080 ;                hash $request_uri;                        }        server       {               listen 80;               server_name your.website.name;                location / {                        proxy_pass        http://your.website.name;                        proxy_set_header   Host             $host;                        proxy_set_header   X-Real-IP        $remote_addr;                        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;               }                log_format www_gjw_com '$remote_addr - $remote_user [$time_local] $request '                                 '"$status" $body_bytes_sent "$http_referer" '                                 '"$http_user_agent" "$http_x_forwarded_for"';               #access_log /data1/logs/www.log www_gjw_com;                location /nginx_status {                    stub_status on;                                                    access_log   off;               }    } }        -------------------[E]url_hash转发方式nginx.conf[E]-------------------- 

 


9、添加nginx启动、关闭、重启脚本

Java代码
  1. -------------------[S]url_hash转发方式nginx启动、关闭脚本[S]--------------------
  2.  
  3. #!/bin/sh
  4.  
  5. CWD=`pwd`
  6.  
  7. case $1 in
  8. start)
  9. /usr/local/webserver/nginx/sbin/nginx
  10. ;;
  11. stop)
  12. kill -2 `ps -ef|grep "/usr/local/webserver/nginx/sbin/nginx"|grep -v "grep"|awk '{print $2}' `
  13. ;;
  14. restart)
  15. cd "$CMD"
  16. $0 stop
  17. $0 start
  18. ;;
  19. *)
  20. echo $"Usage: $0 {start|stop|restart}"
  21. exit 1
  22. esac
  23. exit 0
  24.  
  25. -------------------[E]url_hash转发方式nginx启动、关闭脚本[E]--------------------
-------------------[S]url_hash转发方式nginx启动、关闭脚本[S]--------------------  #!/bin/sh  CWD=`pwd`     case $1 in         start)                         /usr/local/webserver/nginx/sbin/nginx    ;;         stop)                 kill -2 `ps -ef|grep "/usr/local/webserver/nginx/sbin/nginx"|grep -v "grep"|awk '{print $2}' `                         ;;         restart)                 cd "$CMD"                 $0 stop                 $0 start         ;;         *)         echo $"Usage: $0 {start|stop|restart}"         exit 1 esac exit 0   -------------------[E]url_hash转发方式nginx启动、关闭脚本[E]-------------------- 

 

更改该文件权限

Java代码
  1. chmod u+x nginx.sh
  2.  
  3. 启动 nginx ./nginx.sh start
  4. 重启 nginx ./nginx.sh restart
  5. 关闭 nginx ./nginx.sh stop

你可能感兴趣的:(nginx,配置url_hash,转发方式)