我是靠谱客的博主 深情万宝路,最近开发中收集的这篇文章主要介绍Nginx单节点多域名配置SSL使用场景项目实例,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

2019独角兽企业重金招聘Python工程师标准>>> hot3.png


#user  nobody;
worker_processes  4;

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

    #gzip  on;


    server {
        listen       80;
        server_name  www.whxunbao.com;
	rewrite ^ https://$http_host$request_uri? permanent;    # force redirect http to https

        #charset koi8-r;

        access_log  C:/virtualhost/WenBoSystem/test/nginx-1.12.2/nginx-1.12.2/logs/access.log;
	
        location / {
            root   C:/virtualhost/WenBoSystem/test/ylw/dist; #静态资源根目录
            index  index.html index.htm; #index文件
	    try_files $uri $uri/ /index.html;#根据官网这规则配置
        }
	#前后端路由分离部署Nginx配置#
	location /api {
	    rewrite ^/api/(.*)$ /$1 break;
            proxy_pass  http://localhost:8087;
	    proxy_redirect off;
            proxy_set_header Host $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  www.whxunbao.com;
	ssl    on;
        ssl_certificate      C:/virtualhost/WenBoSystem/test/ylw/www.whxunbao.com证书/Nginx/1_www.whxunbao.com_bundle.crt;
        ssl_certificate_key  C:/virtualhost/WenBoSystem/test/ylw/www.whxunbao.com证书/Nginx/2_www.whxunbao.com.key;

        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
	#设置长连接
        keepalive_timeout   70;
        ##location / {
          #  root   html;
           # index  index.html index.htm;
        #}
	location / {
            root   C:/virtualhost/WenBoSystem/test/ylw/dist; #静态资源根目录
            index  index.html index.htm; #index文件
	    try_files $uri $uri/ /index.html;#根据官网这规则配置  
        }
	#前后端路由分离部署Nginx配置#
	location /api {
	    rewrite ^/api/(.*)$ /$1 break;
            proxy_pass   http://localhost:8087;
	    proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }



############################################### other Web Application  ####################################################

    server {
        listen       80;
        server_name  www.museumguide.cn;
	#rewrite ^ https://$http_host$request_uri? permanent;    # force redirect http to https
	rewrite ^(.*)$  https://$host$1 permanent;

        #charset koi8-r;

        access_log  C:/virtualhost/WenBoSystem/test/nginx-1.12.2/nginx-1.12.2/logs/access.log;
	
	#前后端路由分离部署Nginx配置#
	location / {
            proxy_pass  http://www.museumguide.cn:8099/Hmdl;
	    proxy_redirect off;
            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 https;
        }

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


    server {
        listen       443;
        server_name  www.museumguide.cn;
	ssl    on;
        ssl_certificate      C:/virtualhost/WenBoSystem/test/Nginx/1_www.museumguide.cn_bundle.crt;
        ssl_certificate_key  C:/virtualhost/WenBoSystem/test/Nginx/2_www.museumguide.cn.key;

        ssl_session_cache    shared:SSL:10m;
        ssl_session_timeout  10m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
	#设置长连接
        keepalive_timeout   70;

	location / {
            proxy_pass  http://www.museumguide.cn:8099;
	    proxy_redirect off;
            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 https;
	    proxy_next_upstream   off;
        }
 

       
    }

}

使用场景

在单台腾讯云服务器上同时运行多个项目,每个项目有单独的域名,各个域名对应的SSL证书也已经申请好。这种情况下需要使用JNI的方式配置多个SSL访问。

项目实例

1.其中一个项目是前后分离,前端Vue构建的html静态画面(打包后的dist文件),后端springboot服务接口。在Nginx里配置强制跳转SSL连接,使用http方式访问被代理的web应用。

2.另一个项目是普通的服务端渲染web应用。使用Tomcat进行部署,在Nginx里配置强制跳转SSL连接,使用http方式访问被代理的web应用。

同时需要修改tomcat的默认配置。

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <!--<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />-->
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8099" protocol="HTTP/1.1"  
                 connectionTimeout="20000"  
                redirectPort="443" /> 
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="www.museumguide.cn">

      <!--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 name="www.museumguide.cn"  appBase="webapps"
            unpackWARs="true" autoDeploy="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"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
			   <Context docBase="C:virtualhostWenBoSystemapache-tomcat-7.0.81webappsHmdl" 
                 path="/" reloadable="true"/>
		<Valve className="org.apache.catalina.valves.RemoteIpValve"  
                  remoteIpHeader="x-forwarded-for"  
                   remoteIpProxiesHeader="x-forwarded-by"  
                   protocolHeader="x-forwarded-proto" /> 

      </Host>
    </Engine>
  </Service>
</Server>

3.需要保证host文件中 配置了localhost选项

转载于:https://my.oschina.net/odetteisgorgeous/blog/1616601

最后

以上就是深情万宝路为你收集整理的Nginx单节点多域名配置SSL使用场景项目实例的全部内容,希望文章能够帮你解决Nginx单节点多域名配置SSL使用场景项目实例所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(82)

评论列表共有 0 条评论

立即
投稿
返回
顶部