【Web后端笔记】windows下nginx的基本配置和使用

时间:2022-08-28 13:37:53

nginx是一个轻便好用的服务器,在代理、均衡等方面比较强大,详情自行百度。

1、下载:

去官网下载windows的版本就是 http://nginx.org/en/download.html 我选的是稳定版1.8.0

【Web后端笔记】windows下nginx的基本配置和使用

2、解压:

下载完是一个压缩包,解压后就可以直接用的了

【Web后端笔记】windows下nginx的基本配置和使用

3、使用:

用命令行切换到文件目录下,输入start nginx,有一个黑框闪一下然后消失而不会像tomcat那样保留在那,就启动了:

【Web后端笔记】windows下nginx的基本配置和使用

浏览器输入localhost,如果有欢迎界面就是启动成功了:

【Web后端笔记】windows下nginx的基本配置和使用

4、配置:

最常用的就是配置conf文件夹里的nginx.conf,在这里可以配置很多东西,详细的自己去查吧=,=

【Web后端笔记】windows下nginx的基本配置和使用

5、常用命令:

启动:start nginx

更新conf文件后直接reload,不用重启:nginx -s reload

关闭:nginx -s quit

检查conf文件是否有错:nginx -t

6、完成代理,监听80端口将不同域名绑定到tomcat上的不同端口的不同项目:

这里一定要注意!!如果tomcat(或其他程序)占用了80端口,这里再监听会不行!!我弄了好久都不行,就是这个问题!(自己sb了)

这是我的nginx.conf,其实重点就是每个server的配置

  1 #user  nobody;
  2 worker_processes  1;
  3 
  4 #error_log  logs/error.log;
  5 #error_log  logs/error.log  notice;
  6 #error_log  logs/error.log  info;
  7 
  8 #pid        logs/nginx.pid;
  9 
 10 
 11 events {
 12     worker_connections  1024;
 13 }
 14 
 15 
 16 http {
 17     include       mime.types;
 18     default_type  application/octet-stream;
 19 
 20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 21     #                  '$status $body_bytes_sent "$http_referer" '
 22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
 23 
 24     #access_log  logs/access.log  main;
 25 
 26     sendfile        on;
 27     #tcp_nopush     on;
 28 
 29     #keepalive_timeout  0;
 30     keepalive_timeout  65;
 31 
 32     #gzip  on;
 33 
 34 server {
 35         listen       80;
 36         server_name  www.workmap.net.cn;
 37 
 38         #charset koi8-r;
 39 
 40         #access_log  logs/host.access.log  main;
 41 
 42         location / {
 43     proxy_pass http://127.0.0.1:8080/;
 44     proxy_set_header   Host    $host;
 45     proxy_set_header   X-Real-IP   $remote_addr;
 46     proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 47     }
 48 
 49         #error_page  404              /404.html;
 50 
 51         # redirect server error pages to the static page /50x.html
 52         #
 53         error_page   500 502 503 504  /50x.html;
 54         location = /50x.html {
 55             root   html;
 56         }
 57 
 58         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
 59         #
 60         #location ~ \.php$ {
 61         #    proxy_pass   http://127.0.0.1;
 62         #}
 63 
 64         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 65         #
 66         #location ~ \.php$ {
 67         #    root           html;
 68         #    fastcgi_pass   127.0.0.1:9000;
 69         #    fastcgi_index  index.php;
 70         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
 71         #    include        fastcgi_params;
 72         #}
 73 
 74         # deny access to .htaccess files, if Apache's document root
 75         # concurs with nginx's one
 76         #
 77         #location ~ /\.ht {
 78         #    deny  all;
 79         #}
 80     }
 81 
 82     server {
 83         listen       80;
 84         server_name  tpshare.cn www.tpshare.cn;
 85 
 86         #charset koi8-r;
 87 
 88         #access_log  logs/host.access.log  main;
 89 
 90         location / {
 91     proxy_pass http://127.0.0.1:9090/;
 92     proxy_set_header   Host    $host;
 93     proxy_set_header   X-Real-IP   $remote_addr;
 94     proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
 95     } 
 96      }
 97 
 98      
 99 
100 
101     # another virtual host using mix of IP-, name-, and port-based configuration
102     #
103     #server {
104     #    listen       8000;
105     #    listen       somename:8080;
106     #    server_name  somename  alias  another.alias;
107 
108     #    location / {
109     #        root   html;
110     #        index  index.html index.htm;
111     #    }
112     #}
113 
114 
115     # HTTPS server
116     #
117     #server {
118     #    listen       443 ssl;
119     #    server_name  localhost;
120 
121     #    ssl_certificate      cert.pem;
122     #    ssl_certificate_key  cert.key;
123 
124     #    ssl_session_cache    shared:SSL:1m;
125     #    ssl_session_timeout  5m;
126 
127     #    ssl_ciphers  HIGH:!aNULL:!MD5;
128     #    ssl_prefer_server_ciphers  on;
129 
130     #    location / {
131     #        root   html;
132     #        index  index.html index.htm;
133     #    }
134     #}
135 
136 }

另外tomcat配置文件也要有相应的配置:

Root下的index.html

1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5     <head><title>tpshare</title></head>
6 <body>
7 <script language='javascript'>document.location = 'http://www.tpshare.cn:9090/SharePDF/'</script>
8 </body>
9 </html>

conf文件夹下的server.xml:

  1 <?xml version='1.0' encoding='utf-8'?>
  2 <!--
  3   Licensed to the Apache Software Foundation (ASF) under one or more
  4   contributor license agreements.  See the NOTICE file distributed with
  5   this work for additional information regarding copyright ownership.
  6   The ASF licenses this file to You under the Apache License, Version 2.0
  7   (the "License"); you may not use this file except in compliance with
  8   the License.  You may obtain a copy of the License at
  9 
 10       http://www.apache.org/licenses/LICENSE-2.0
 11 
 12   Unless required by applicable law or agreed to in writing, software
 13   distributed under the License is distributed on an "AS IS" BASIS,
 14   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15   See the License for the specific language governing permissions and
 16   limitations under the License.
 17 -->
 18 <!-- Note:  A "Server" is not itself a "Container", so you may not
 19      define subcomponents such as "Valves" at this level.
 20      Documentation at /docs/config/server.html
 21  -->
 22 <Server port="8205" shutdown="SHUTDOWN">
 23 
 24   <!--APR library loader. Documentation at /docs/apr.html -->
 25   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 26   <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->
 27   <Listener className="org.apache.catalina.core.JasperListener" />
 28   <!-- Prevent memory leaks due to use of particular java/javax APIs-->
 29   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 30   <!-- JMX Support for the Tomcat server. Documentation at /docs/non-existent.html -->
 31   <Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />
 32   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 33 
 34   <!-- Global JNDI resources
 35        Documentation at /docs/jndi-resources-howto.html
 36   -->
 37   <GlobalNamingResources>
 38     <!-- Editable user database that can also be used by
 39          UserDatabaseRealm to authenticate users
 40     -->
 41     <Resource name="UserDatabase" auth="Container"
 42               type="org.apache.catalina.UserDatabase"
 43               description="User database that can be updated and saved"
 44               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 45               pathname="conf/tomcat-users.xml" />
 46   </GlobalNamingResources>
 47 
 48   <!-- A "Service" is a collection of one or more "Connectors" that share
 49        a single "Container" Note:  A "Service" is not itself a "Container", 
 50        so you may not define subcomponents such as "Valves" at this level.
 51        Documentation at /docs/config/service.html
 52    -->
 53   <Service name="Catalina">
 54   
 55     <!--The connectors can use a shared executor, you can define one or more named thread pools-->
 56     <!--
 57     <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" 
 58         maxThreads="150" minSpareThreads="4"/>
 59     -->
 60     
 61     
 62     <!-- A "Connector" represents an endpoint by which requests are received
 63          and responses are returned. Documentation at :
 64          Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
 65          Java AJP  Connector: /docs/config/ajp.html
 66          APR (HTTP/AJP) Connector: /docs/apr.html
 67          Define a non-SSL HTTP/1.1 Connector on port 8080
 68     -->
 69     <Connector port="9090" protocol="HTTP/1.1" 
 70                connectionTimeout="20000" 
 71                URIEncoding="utf-8"
 72                redirectPort="8443" />
 73     <!-- A "Connector" using the shared thread pool-->
 74     <!--
 75     <Connector executor="tomcatThreadPool"
 76                port="8080" protocol="HTTP/1.1" 
 77                connectionTimeout="20000" 
 78         URIEncoding="utf-8"
 79                redirectPort="8443" />
 80     -->           
 81     <!-- Define a SSL HTTP/1.1 Connector on port 8443
 82          This connector uses the JSSE configuration, when using APR, the 
 83          connector should be using the OpenSSL style configuration
 84          described in the APR documentation -->
 85     <!--
 86     <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true"
 87                maxThreads="150" scheme="https" secure="true"
 88                clientAuth="false" sslProtocol="TLS" />
 89     -->
 90 
 91     <!-- Define an AJP 1.3 Connector on port 8009 -->
 92     <Connector port="8109" protocol="AJP/1.3" redirectPort="8443" />
 93 
 94 
 95     <!-- An Engine represents the entry point (within Catalina) that processes
 96          every request.  The Engine implementation for Tomcat stand alone
 97          analyzes the HTTP headers included with the request, and passes them
 98          on to the appropriate Host (virtual host).
 99          Documentation at /docs/config/engine.html -->
100 
101     <!-- You should set jvmRoute to support load-balancing via AJP ie :
102     <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">         
103     --> 
104     <Engine name="Catalina" defaultHost="localhost">
105 
106       <!--For clustering, please take a look at documentation at:
107           /docs/cluster-howto.html  (simple how to)
108           /docs/config/cluster.html (reference documentation) -->
109       <!--
110       <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
111       -->        
112 
113       <!-- The request dumper valve dumps useful debugging information about
114            the request and response data received and sent by Tomcat.
115            Documentation at: /docs/config/valve.html -->
116       <!--
117       <Valve className="org.apache.catalina.valves.RequestDumperValve"/>
118       -->
119 
120       <!-- This Realm uses the UserDatabase configured in the global JNDI
121            resources under the key "UserDatabase".  Any edits
122            that are performed against this UserDatabase are immediately
123            available for use by the Realm.  -->
124       <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
125              resourceName="UserDatabase"/>
126 
127       <!-- Define the default virtual host
128            Note: XML Schema validation will not work with Xerces 2.2.
129        -->
130       <Host name="localhost"  appBase="webapps"
131             unpackWARs="true" autoDeploy="true"
132             xmlValidation="false" xmlNamespaceAware="false">
133 
134         <!-- SingleSignOn valve, share authentication between web applications
135              Documentation at: /docs/config/valve.html -->
136         <!--
137         <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
138         -->
139 
140         <!-- Access log processes all example.
141              Documentation at: /docs/config/valve.html -->
142         <!--
143         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"  
144                prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
145         -->
146     <Context path="" docBase="D:\dev_soft\apache-tomcat-6.0.43_9090_sharepdf\webapps\SharePDF">
147                 <WatchedResource>WEB-INF/web.xml</WatchedResource>
148         </Context>
149 
150       </Host>
151     </Engine>
152   </Service>
153 </Server>

这样就可以实现用nginx将不同域名绑定到不同tomcat的不同端口。

参考原文:windows下nginx+tomcat整合的安装与配置

参考原文:nginx实现单ip,多域名,多站点的访问配置教程