0
点赞
收藏
分享

微信扫一扫

nginx做负载tomcat

琛彤麻麻 2022-02-06 阅读 71


第一步:
PCRE 作用是让 Nginx 支持 Rewrite 功能。
其实还需要个 openssl 一般linux上都安装了 所以 此处略去此说明
下载 PCRE 安装包,下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
解压安装
tar zxvf pcre-8.35.tar.gz
编译
./configure
make && make install
查看版本:pcre-config --version
第二步:
下载nginx http://nginx.org/download/nginx-1.6.2.tar.gz
解压
tar zxvf nginx-1.6.2.tar.gz
编译
./configure --prefix=/home/julong/tomcat/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/home/julong/tomcat/pcre-8.35
make
make install(nginx 此目录需要提前创建,并非解压的文件夹)

nginx的配置如下:


第三步:验证配置
[julong@localhost nginx]$ ./sbin/nginx -t
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/julong/tomcat/nginx/conf/nginx.conf:2
nginx: the configuration file /home/julong/tomcat/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/julong/tomcat/nginx/conf/nginx.conf test is successful
第四步:启动nginx
[julong@localhost nginx]$ ./sbin/nginx
在网页试着访问下http://192.168.1.222:9090/
./sbin/nginx -s reload # 重新载入配置文件
./sbin/nginx -s reopen # 重启 Nginx
./sbin/nginx -s stop # 停止 Nginx
nginx配置如下

#使用的用户linux的用户
user root julong;
#进程的数量与cpu保持一致
worker_processes 1;
#错误日志保存的位置
error_log /home/julong/tomcat/nginx/logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid的存放位置
pid /home/julong/tomcat/nginx/nginx.pid;


events {
#使用 epoll 事件模型
use epoll;
#工作的最大并发数
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 调用来快速的响应客户端
sendfile on;
tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;
#配置监听的服务器
upstream www.safein.cn {
#ip_hash:基于源地址哈希,主要目的是会话保持
ip_hash;
server 192.168.1.222:8080;
server 192.168.1.222:8888;
}
#定义一个虚拟主机
server {
#监听的端口
listen 9090;
#监听的服务名定义虚拟主机名,可以使用多个名称,还可以使用正则表达式或通配符。
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}
location ~ /{
proxy_pass http://www.safein.cn;
proxy_set_header HOST $host:$server_port;

#此方式为访问webservices的方式如果不是webservices项目直接$host
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#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;
# }
#}

}







举报

相关推荐

0 条评论