技术原理

用nssm来管理nginx代理服务

2016-06-21  本文已影响1307人  猫猫_tomluo

软件介绍

了解nginx配置项

[your_nginx_path]\conf\nginx.conf

server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        #}
    }

这里面有几个需要了解的地方:

配置实现

这里我们用nginx实现对tomcat的真实请求,也就是来自于所有来自外部的请求,进入nginx的80端口进行监听,如果静态文件(js,css,image等等),由nginx直接返回,如果是别的请求将由tomcat响应,tomcat只对nginx可见.

软件准备

nssm-2.24.zip
nginx-1.11.1.zip windows版本

加入到环境变量

path=F:\server\nssm-2.24\win64;F:\server\nginx-1.11.1;

准备一个nginx配置文件,静态文件由nginx直接处理,动态文件由8080端口处理

F:\server\for-tomcat-nginx.conf

#user  nobody;
worker_processes  1;

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;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    # 转发的服务器,upstream 为负载均衡做准备
     upstream tomcat_server{ 
            server localhost:8080; 
     } 

    server {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;  
        root  F:/data/wwwtest/test-nginx/target/classes/static;  

        charset UTF-8;

        # 动态请求的转发
        location ~ .*.action$ { 
            proxy_pass http://tomcat_server; 
            proxy_set_header Host $host; 
        } 
        # 静态请求直接读取
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ { 
            expires      30d; 
        }
        #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;
        }
    }
}

配置

命令行
nssm install
会弹出下面的配置界面:

Paste_Image.png

开始服务

nssm start test-nginx
同时在开启你的tomcat服务器

Paste_Image.png

访问/index.action

Paste_Image.png

本文件的demo文件

spring boot+web
test-nginx\src\resources\templates\application.properties

spring.mvc.view.suffix=action

test-nginx\src\test\java\com\example\DemoApplicationTests.java

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@SpringBootApplication
@Controller
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
    @RequestMapping("/index")
    public ModelAndView main(){
        Map map=new HashMap();
        map.put("message","你好,tomLuo!");
        return new ModelAndView("index",map);
    }
}

test-nginx\src\main\resources\template\index.ftl

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>test nginx</title>
</head>
<body>
<p>这是动态页,来自实际的8080端口响应.</p>
<p>${message}</p>
</body>
</html>

test-nginx\src\main\resources\static\index.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>test nginx</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.css" rel="stylesheet">


</head>

<body>

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="/index.action">访问/index.action页</a>
        </div>


    </div><!-- /.container -->
</nav>

<p>test nginx</p>
<img src="image/1.jpg"/>
<img src="image/2.jpg"/>

<script src="js/jquery-1.10.2.js"></script>


</body>
</html>

参考

本项目实验 https://github.com/tomlxq/test-nginx

上一篇 下一篇

猜你喜欢

热点阅读