apache最新版2.4.12在本地没有问题,部署在远程服务器上时常出现500的错误,概率不算小。原因不明,error日志里没有具体的错误。

还是考虑一下使用nginx,方便不少,下面简单介绍一下:

  • nginx的安装与配置
  • nginx代理与反代理
  • nginx支持ssl的配置方法
  • nginx转发给tomcat的http属性不一致怎么办

顺便一笔带过最新版apache的使用,以及nginx和apache编译安装的方法。

1 安装

nginx在线安装即可,apt-get install nginx,最新版本是1.9.3 apache的安装方法也是apt-get install apache2,最新版本是2.4.12

2 配置

打开/etc/nginx/nginx.conf文件,可以看到默认对ssl都已经支持。再打开vim sites-enabled/default添加网站,可以看到已经存在一个默认网站。

检查设置是否正确,使用运令行:nginx -t,使新设置生效运行命令行:nginx -s reload

2.1 代理设置

添加一个新网站,且将nginx作为tomcat的代理和反代理,proxy_pass指向tomcat的工作host就行

server {
    listen 80;
    server_name tl;
    root /var/www/locator_3.0;
    location / {
      proxy_pass http://localhost:8080;
   }
}

2.2 ssl设置

如果期望nginx使用ssl协议,则添加以下几项设置就可以了:

server {
        listen 443;
        server_name tl;
        root /var/www/locator_3.0;
        ssl on;
        ssl_certificate /etc/ssl/certs/server.crt;
        ssl_certificate_key /etc/ssl/private/server.key;
        location / {
                proxy_pass http://localhost:8080;
        }
}

更多nginx配置见这篇

2.3 代理tcp

这个需要手动编译一个stream模块的nginx,见下文的2.6.2

nginx的配置有几块:events、stream、http,将nginx.conf这stream包含一下其它文件:

http {
  include /usr/local/nginx/sites-enabled/*.http;
}

stream {
  include /usr/local/nginx/sites-enabled/*.stream;
}

stream的配置示例如下,比如代理mysql:

upstream mysql {
  zone myapp1 64k;
  server 172.26.71.241:3306 weight=1 max_fails=3 fail_timeout=30s;
}

server {
    listen 3306;
    proxy_connect_timeout 1s;
    proxy_timeout 3s;
    proxy_pass mysql;
}

注意,以上配置的proxy_timeout设置太短了,导致外链频繁中断,可以参考以下配置:

server {
    listen 3306;

    # http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_connect_timeout
    # 与upstream server的连接超时时间
    # Defines a timeout for establishing a connection with a proxied server.
    proxy_connect_timeout 5s;

    # 决定这个udp端口上报数据的时候保持的时间
    # Sets the timeout between two successive read or write operations on client or proxied server connections. If no data is transmitted within this time, the connection is closed.
    proxy_timeout 61s;

    proxy_pass mysql;
}

2.4 解决head不一致的问题

比如外面通过tl来访问网站,但通过localhost跳转到tomcat,tomcat解析到host为localhost,scheme也不一致。

常规的情况没有什么问题,但如果需要对比url这种事情一来,问题显而意见了,具体情况见:HTTPS介绍及应用

解决这个问题需要在/etc/nginx/nginx.conf里(或者配置在site属性里)增加配置项:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

然后在tomcat的server.xml的engine节点下新增一个标签属性:

<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For" protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>

这个也可以配置在host节点下,从字面上看,以上大概就是指定和设置代理转发的http的head属性。这样,如果网页请求https://tl/abc,虽然代理的配置的是proxy_pass http://localhost:8080,但tomcat接收到的url不再是http://localhost:8080,而同样是https://tl/abc

2.5 nginx使用过程中产生的新的问题

  • 如果website部署在tomcat中,在apache+tomcat(1..N)的工作模式中,tomcat可以配置多个域名及网站,即ajp端口对应多个网站。但nginx是代理模式,只能指向tomcat端口的根目录网站,而根目录网站只能有一个。就无法配置nginx+tomcat(1..N)。这个地方也讲述了这个问题:http://www.oschina.net/question/111288_127054

答:这个问题应该是不存在,nginx+tomcat(1..N)的工作模式也是存在的,proxy_pass是指向tomcat的端口,通过server_name匹配tomcat的Server.xml中的enginne节点下面的host(name属性)

  • nginx同样有很多500的错误,原因不详

2.6 其它尝试

2.6.1 卸载在线版nginx(ubuntu下)

主要是以下这些命令:

sudo apt-get remove nginx nginx-common # 卸载删除除了配置文件以外的所有文件。
 
sudo apt-get purge nginx nginx-common # 卸载所有东东,包括删除配置文件。
 
sudo apt-get autoremove # 在上面命令结束后执行,主要是卸载删除Nginx的不再被使用的依赖包。
 
sudo apt-get remove nginx-full nginx-common #卸载删除两个主要的包。

2.6.2 编译安装nginx

(1)下载

#wget http://nginx.org/download/nginx-1.9.3.tar.gz
#tar -xvf nginx-1.9.3.tar.gz
#cd nginx-1.9.3/

更多下载地址在这里,最新版1.17.7(2019-12-25)

(2)生成配置

./configure --with-http_ssl_module
或 ./configure --user=root --group=root --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-stream --with-stream_ssl_module

错误提示与处理:

  • 提示:./configure: error: the HTTP rewrite module requires the PCRE library.

解决办法:安装依赖(同前文)apt-get install libpcre3 libpcre3-dev

  • 提示:./configure: error: SSL modules require the OpenSSL library.

解决办法:安装依赖apt-get install libssl-dev

  • 提示:./configure: error: the HTTP gzip module requires the zlib library.

安装依赖:apt-get install --reinstall zlibc zlib1g zlib1g-dev

centos是这样的:yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc

.configure后,控制台生成如下:

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

(3)编译: make && make install

更多编译安装可以参考这里 ,以及官方文档

3、编译安装和配置apache

下载并编译apache

解压和编译依赖的库
#tar -xvf apr-1.4.5.tar.gz
#./configure --prefix=/usr/local/apr
#make & make install
出错了则(make clean)

#tar -xvf  apr-util-1.3.12.tar.gz
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
#make & make install

#unzip pcre-8.10.zip
./configure --prefix=/usr/local/pcre
#make & make install

解压编译apache(支持ssl)
#tar -xvf httpd-2.4.4.tar.bz2
#./configure --prefix=/usr/local/httpd2.4.4 --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre --enable-ssl

如果提示

 checking for OpenSSL version >= 0.9.7… FAILED

则需要

apt-get install libssl-dev

下载并编译tomcat-connector

下载
#wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.41-src.tar.gz
#tar -xvf tomcat-connectors-1.2.41-src.tar.gz
#cd tomcat-connectors-1.2.41-src/
编译
#./configure --with-apxs=/home/apache/httpd-2.4.4/support/apxs
#make
#make install
快捷标示版本(http head里也能看到)
#ln -s /usr/local/httpd2.4.4/modules/mod_jk.so /usr/local/httpd2.4.4/modules/mod_jk.so.1.2.41
启动生效
#/usr/local/httpd2.4.4/bin/httpd -k restart

mod_jk的相关配置 mod_jk.conf(被httpd.conf包含)

JkWorkersFile conf/extra/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"

worker.proprties

worker.list=worker1,worker2

worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=50
worker.worker1.cachesize=10
worker.worker1.cache_timeout=600
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300

worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8010
worker.worker2.lbfactor=50
worker.worker2.cachesize=10
worker.worker2.cache_timeout=600
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300

vhost的ssl站点的配置方法:

<VirtualHost *:443>
   ServerName xxx
    #DocumentRoot /var/www/xxx 
    SSLEngine On
    SSLOptions +StrictRequire
    SSLCertificateFile conf/server.crt
    SSLCertificateKeyFile conf/server.key
    JkMount /* worker1
    ErrorLog logs/ssl_err.log
    CustomLog logs/ssl_access.log combined
</VirtualHost>

vhost普通站点的配置方法:

<VirtualHost *:80>
    ServerName xxx
    ServerAlias xxx
    DocumentRoot /var/www/xxx
    JkMount /* worker1
    ErrorLog logs/error.log
    CustomLog logs/access.log combined
    DirectoryIndex index.html
</VirtualHost>