最后的配置部分:LNMP+Tomcat

Nginx与PHP部分

mkdir /www/php -p
echo -e "<?php\n\tphpinfo();\n?>" > /www/php/index.php
vim /usr/local/nginx/conf/nginx.conf
#==============Nginx代理PHP端===================
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#上面那个注视掉,改成下面的不然就出FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream这个错
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
}

Nginx+Tomcat部分

mkdir /www/jsp -p
echo "this is jsp" >>/www/jsp/index.jsp
echo "this is jsp" >>/www/do/index.do
vim /usr/local/tomcat/conf/server.xml

配置Tomcat中的server.conf,修改tomcat家目录

vim /usr/local/tomcat/conf/server.xml
<!-- 索搜此项修改默认WEB端口,这里默认保持不变 -->
    <Connector port="8080" protocol="HTTP/1.1" 
               connectionTimeout="20000"
               redirectPort="8443" />
<!-- 可以修改域名或者IP,但作代理请保持localhost -->
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
<!-- 新增此项修改WEB的家目录 -->
        <Context path="" docBase="/www"></Context>

配置nginx中的nginx.conf,让其能代理jsp网页

#==========Nginx代理JSP段================
        location ~ (\.jsp)|(\.do)$ {
            index index.jsp;
            proxy_pass http://127.0.0.1:8080;   #来自jsp请求交给tomcat处理
            proxy_redirect off;
            proxy_set_header Host $host;    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            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;   #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_read_timeout 90;      #连接成功后,后端服务器响应时间(代理接收超时)
            proxy_buffer_size 4k;       #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            proxy_buffers 6 32k;        #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
            proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)
            proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }
        location ~ .*\.(gif|jpg|png|bmp|swf)$   #由nginx处理静态页面
        {
        expires 30d;   #使用expires缓存模块,缓存到客户端30天
        }
        error_page  404              /404.html;   #错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

------------------------------------------

完整的nginx.conf文件,仅供参考

user  www;
worker_processes  1;
error_log  logs/error.log;
pid        logs/nginx.pid;
events {
    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;
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k; #客户端请求头部的缓冲区大小,一般一个请求头的大小不会超过1k
    large_client_header_buffers 4 32k; #客户请求头缓冲大小 nginx默认会用client_header_buffer_size这个buffer来读取header值
    client_max_body_size 8m; #设定通过nginx上传文件的大小
    sendfile        on;
    keepalive_timeout  65;

 #===================重要位置============
    fastcgi_connect_timeout 300; #指定连接到后端FastCGI的超时时间。
    fastcgi_send_timeout 300; #向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送>请求的超时时间。
    fastcgi_read_timeout 300; #接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答>的超时时间。
    fastcgi_buffer_size 254k; #指定读取FastCGI应答第一部分需要用多大的缓冲区
    fastcgi_buffers 16 256k; #指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答。
    fastcgi_busy_buffers_size 512k; #这个指令我也不知道是做什么用,只知道默认值是fastcgi_buffers的两倍
。
    fastcgi_temp_file_write_size 512k; #在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。
    #gzip  on;
    gzip              on; #该指令用于开启或关闭gzip模块(on/off)
    gzip_min_length   1k; #设置允许压缩的页面最小字节数,页面字节数从header头得content-length中进行获>取
    gzip_buffers      4 16k; #设置系统获取几个单位的缓存用于存储gzip的压缩结果数据流
    gzip_http_version 1.0; #识别http的协议版本
    gzip_comp_level   2;   #gzip压缩比,1压缩比最小处理速度最快
         #匹配mime类型进行压缩,无论是否指定,”text/html”类型总是会被压缩的
    gzip_types        text/plain application/x-javascript text/css application/xml text/javascript;
    gzip_vary         on; #和http头有关系,加个vary头,给代理服务器用的
    server {
        listen       80;
        server_name 自己的网站名;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        root /www;
        location / {
            index  index.html index.htm index.jsp index.php;
        }
#===========PHP段=================
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include        fastcgi_params;
        }
#============Tomcat段=============
        location ~ (\.jsp)|(\.do)$ {
            index index.jsp;
            proxy_pass http://127.0.0.1:8080;   #来自jsp请求交给tomcat处理
            proxy_redirect off;
            proxy_set_header Host $host;    #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
            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;   #nginx跟后端服务器连接超时时间(代理连接超时)
            proxy_read_timeout 90;      #连接成功后,后端服务器响应时间(代理接收超时)
            proxy_buffer_size 4k;       #设置代理服务器(nginx)保存用户头信息的缓冲区大小
            proxy_buffers 6 32k;        #proxy_buffers缓冲区,网页平均在32k以下的话,这样设置
            proxy_busy_buffers_size 64k;#高负荷下缓冲大小(proxy_buffers*2)
            proxy_temp_file_write_size 64k; #设定缓存文件夹大小,大于这个值,将从upstream服务器传
        }
        location ~ .*\.(gif|jpg|png|bmp|swf)$   #由nginx处理静态页面
        {
        expires 30d;   #使用expires缓存模块,缓存到客户端30天
        }
        error_page  404              /404.html;   #错误页面
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }


ok,就到这里了

参考:Linux下Nginx+Tomcat整合的安装与配置

 

posted on 2016-09-14 15:27  losbyday  阅读(1463)  评论(0编辑  收藏  举报

导航