tomcat+nginx部署Javaweb项目,并实现通过域名访问本地项目

本地环境tomcat8、jdk1.8、nginx1.16

要实现的功能,使用nginx反向代理,实现域名访问本地项目,且不用在浏览器url中输入项目名称;

项目名称:njuresearch,测试域名:weixin.lichaolong.com

第一种方式:url中携带项目名称njuresearch

第一步:配置hosts文件,增加一行

127.0.0.1       weixin.lichaolong.com

第二步:配置nginx的配置文件nginx.conf文件

文件内容如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    upstream tomcat_server{ 
        server 127.0.0.1:8080; 
    }

    server {
        listen       8098;
        server_name  weixin.lichaolong.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /njuresearch/ {
            proxy_pass http://127.0.0.1:8080/njuresearch/;
            index index.jsp;
        }


        # 动态请求jsp的转发
        location ~ .*.jsp$ { 
            #proxy_pass http://127.0.0.1:8080/njuresearch/;
            proxy_pass http://tomcat_server; 
            proxy_set_header Host $host; 
        }

        
        
        #静态请求直接读取
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|html|min.css|mustache)$ { 
            #proxy_pass http://127.0.0.1:8080/njuresearch/;
            proxy_pass http://tomcat_server; 
            root D:\tomcat\apache-tomcat-8.0.47\webapps\njuresearch\static;
            expires  30d; 
        }

        #静态请求直接读取
        location ~ .*\.(js|min.js|mustache)$ { 
            #proxy_pass http://127.0.0.1:8080/njuresearch/;
            proxy_pass http://tomcat_server; 
            root D:\tomcat\apache-tomcat-8.0.47\webapps\njuresearch\script;
            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;
        }

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

}
 

第三步,配置tomcat下conf目录,server.xml文件

主要内容如下:

<Engine defaultHost="weixin.lichaolong.com" name="Catalina">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
      </Realm>

      <Host appBase="webapps" autoDeploy="true" name="weixin.lichaolong.com" unpackWARs="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>
            <Context docBase="njuresearch" path="/njuresearch" reloadable="true" source="org.eclipse.jst.jee.server:njuresearch"/>
        </Host>
    </Engine>

注意:path=“/njuresearch”,为空,写上项目名称/njuresearch

重启tomcat与nginx即可,在浏览器中输入

http://weixin.lichaolong.com:8098/njuresearch/index.jsp即可;

第二种方式:浏览器url中无需输入项目名称njuresearch

第一步:同第一种方式一样;

第二步:nginx.conf配置内容


第三步:tomcat下conf目录server.xml

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Nginx是一种轻量级的Web服务器和反向代理服务器。它可以用于部署JavaWeb项目。以下是部署JavaWeb项目的步骤: 1. 首先,你需要安装Nginx。你可以通过以下命令来安装Nginx: ``` cd /usr/local/source wget http://nginx.org/download/nginx-1.9.0.tar.gz tar -zxvf nginx-1.9.0.tar.gz cd nginx-1.9.0 ./configure make make install ``` 安装完成后,你可以使用`nginx -v`命令来检查Nginx的版本。 2. 接下来,你需要配置Nginx部署JavaWeb项目。你可以通过编辑Nginx配置文件来实现。配置文件通常位于`/usr/local/nginx/conf/nginx.conf`。你可以使用文本编辑器打开该文件。 3. 在配置文件中,你需要设置Nginx监听的端口号和服务器称。你可以使用以下语法来设置监听端口和服务器称: ``` server { listen 80; server_name your_domain.com; } ``` 4. 然后,你需要配置Nginx反向代理JavaWeb项目。你可以使用以下语法来设置反向代理: ``` location / { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } ``` 在上面的例子中,我们假设JavaWeb项目运行在本地的8080端口上。你可以根据实际情况进行相应的更改。 5. 配置完成后,保存并退出配置文件。然后,你可以使用以下命令重启Nginx以使配置生效: ``` nginx -s reload ``` 现在,你的JavaWeb项目就可以通过Nginx访问了。你可以使用IP地址或域名访问部署项目页面。请确保你的JavaWeb项目在指定的端口上正在运行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值