springboot之修改内置tomcat配置项

1.spring boot默认端口号是8080,如果要修改端口的话,只需要修改application.properties文件,在其中加入

  例如: server.port=8081

2.在正常的项目中,项目路径都是localhost:8080+项目名称+相应的访问路径,在spring boot中默认并没有项目名称而是直接访问localhost:8080/访问路径,这种方式的路径格式肯定是不好的,因此我们要为项目指定context-path,只需要在spplication.properties文件中加入:

例如:server.context-path=/springboot

最近踩了一个坑,再导入别人的项目时,用外置的tomcat或者jboss启动都是正常的,在eclipse中用main函数启动内置tomcat时无法访问,出现access deny的情况。在百度和google了无数资料后,最终发现内置tomcat默认不会加上项目名称,需要手动配置上述代码,当然这取决于你前端的请求是否加上了项目名称。

3.指定tomcat的编码格式

        server.tomcat.uri-encoding=UTF-8

以下是一些其他的配置信息,希望能够用的上:

参数配置容器

server.xx开头的是所有servlet容器通用的配置,server.tomcat.xx开头的是tomcat特有的参数,其它类似。

所有参数绑定配置类:org.springframework.boot.autoconfigure.web.ServerProperties

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
server.address= # Network address to which the server should bind to.
server.compression.enabled=false # If response compression is enabled.
server.compression.excluded-user-agents= # List of user-agents to exclude from compression.
server.compression.mime-types= # Comma-separated list of MIME types that should be compressed. For instance `text/html,text/css,application/json`
server.compression.min-response-size= # Minimum response size that is required for compression to be performed. For instance 2048
server.connection-timeout= # Time in milliseconds that connectors will wait for another HTTP request before closing the connection. When not set, the connector's container-specific default will be used. Use a value of -1 to indicate no (i.e. infinite) timeout.
server.display-name=application # Display name of the application.
server.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.error.include-exception=false # Include the "exception" attribute.
server.error.include-stacktrace=never # When to include a "stacktrace" attribute.
server.error.path=/error # Path of the error controller.
server.error.whitelabel.enabled=true # Enable the default error page displayed in browsers in case of a server error.
server.jetty.acceptors= # Number of acceptor threads to use.
server.jetty.accesslog.append=false # Append to log.
server.jetty.accesslog.date-format=dd/MMM/yyyy:HH:mm:ss Z # Timestamp format of the request log.
server.jetty.accesslog.enabled=false # Enable access log.
server.jetty.accesslog.extended-format=false # Enable extended NCSA format.
server.jetty.accesslog.file-date-format= # Date format to place in log file name.
server.jetty.accesslog.filename= # Log filename. If not specified, logs will be redirected to "System.err".
server.jetty.accesslog.locale= # Locale of the request log.
server.jetty.accesslog.log-cookies=false # Enable logging of the request cookies.
server.jetty.accesslog.log-latency=false # Enable logging of request processing time.
server.jetty.accesslog.log-server=false # Enable logging of the request hostname.
server.jetty.accesslog.retention-period=31 # Number of days before rotated log files are deleted.
server.jetty.accesslog.time-zone=GMT # Timezone of the request log.
server.jetty.max-http-post-size=0 # Maximum size in bytes of the HTTP post or put content.
server.jetty.selectors= # Number of selector threads to use.
server.port=8080 # Server HTTP port.
server.server-header= # Value to use for the Server response header (no header is sent if empty)
server.use-forward-headers= # If X-Forwarded-* headers should be applied to the HttpRequest.
server.servlet.context-parameters.*= # Servlet context init parameters
server.servlet.context-path= # Context path of the application.
server.servlet.jsp.class-name=org.apache.jasper.servlet.JspServlet # The class name of the JSP servlet.
server.servlet.jsp.init-parameters.*= # Init parameters used to configure the JSP servlet
server.servlet.jsp.registered=true # Whether or not the JSP servlet is registered
server.servlet.path=/ # Path of the main dispatcher servlet.
server.session.cookie.comment= # Comment for the session cookie.
server.session.cookie.domain= # Domain for the session cookie.
server.session.cookie.http-only= # "HttpOnly" flag for the session cookie.
server.session.cookie.max-age= # Maximum age of the session cookie in seconds.
server.session.cookie.name= # Session cookie name.
server.session.cookie.path= # Path of the session cookie.
server.session.cookie.secure= # "Secure" flag for the session cookie.
server.session.persistent=false # Persist session data between restarts.
server.session.servlet.filter-order=-2147483598 # Session repository filter order.
server.session.servlet.filter-dispatcher-types=ASYNC, ERROR, REQUEST # Session repository filter dispatcher types.
server.session.store-dir= # Directory used to store session data.
server.session.timeout= # Session timeout in seconds.
server.session.tracking-modes= # Session tracking modes (one or more of the following: "cookie", "url", "ssl").
server.ssl.ciphers= # Supported SSL ciphers.
server.ssl.client-auth= # Whether client authentication is wanted ("want") or needed ("need"). Requires a trust store.
server.ssl.enabled= # Enable SSL support.
server.ssl.enabled-protocols= # Enabled SSL protocols.
server.ssl.key-alias= # Alias that identifies the key in the key store.
server.ssl.key-password= # Password used to access the key in the key store.
server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file).
server.ssl.key-store-password= # Password used to access the key store.
server.ssl.key-store-provider= # Provider for the key store.
server.ssl.key-store-type= # Type of the key store.
server.ssl.protocol=TLS # SSL protocol to use.
server.ssl.trust-store= # Trust store that holds SSL certificates.
server.ssl.trust-store-password= # Password used to access the trust store.
server.ssl.trust-store-provider= # Provider for the trust store.
server.ssl.trust-store-type= # Type of the trust store.
server.tomcat.accept-count= # Maximum queue length for incoming connection requests when all possible request processing threads are in use.
server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
server.tomcat.accesslog.request-attributes-enabled=false # Set request attributes for IP address, Hostname, protocol and port used for the request.
server.tomcat.accesslog.rotate=true # Enable access log rotation.
server.tomcat.accesslog.suffix=.log # Log file name suffix.
server.tomcat.additional-tld-skip-patterns= # Comma-separated list of additional patterns that match jars to ignore for TLD scanning.
server.tomcat.background-processor-delay=30 # Delay in seconds between the invocation of backgroundProcess methods.
server.tomcat.basedir= # Tomcat base directory. If not specified a temporary directory will be used.
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
        169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
        127\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.1[6-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.2[0-9]{1}\\.\\d{1,3}\\.\\d{1,3}|\\
        172\\.3[0-1]{1}\\.\\d{1,3}\\.\\d{1,3} # regular expression matching trusted IP addresses.
server.tomcat.max-connections= # Maximum number of connections that the server will accept and process at any given time.
server.tomcat.max-http-header-size=0 # Maximum size in bytes of the HTTP message header.
server.tomcat.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.
server.tomcat.max-threads=0 # Maximum amount of worker threads.
server.tomcat.min-spare-threads=0 # Minimum amount of worker threads.
server.tomcat.port-header=X-Forwarded-Port # Name of the HTTP header used to override the original port value.
server.tomcat.protocol-header= # Header that holds the incoming protocol, usually named "X-Forwarded-Proto".
server.tomcat.protocol-header-https-value=https # Value of the protocol header that indicates that the incoming request uses SSL.
server.tomcat.redirect-context-root= # Whether requests to the context root should be redirected by appending a / to the path.
server.tomcat.remote-ip-header= # Name of the http header from which the remote ip is extracted. For instance `X-FORWARDED-FOR`
server.tomcat.uri-encoding=UTF-8 # Character encoding to use to decode the URI.
server.undertow.accesslog.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Enable access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs.
server.undertow.accesslog.prefix=access_log. # Log file name prefix.
server.undertow.accesslog.rotate=true # Enable access log rotation.
server.undertow.accesslog.suffix=log # Log file name suffix.
server.undertow.buffer-size= # Size of each buffer in bytes.
server.undertow.direct-buffers= # Allocate buffers outside the Java heap.
server.undertow.io-threads= # Number of I/O threads to create for the worker.
server.undertow.eager-filter-init=true # Whether servlet filters should be initialized on startup.
server.undertow.max-http-post-size=0 # Maximum size in bytes of the HTTP post content.
server.undertow.worker-threads= # Number of worker threads.

替换Tomcat

spring-boot-starter-web brings Tomcat with spring-boot-starter-tomcat, but spring-boot-starter-jetty and spring-boot-starter-undertow can be used instead.

spring-boot-starter-web自动携带了tomcat依赖,但也可以替换成jetty和undertow,下面是一个替换jetty的示例。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Exclude the Tomcat dependency -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
### 回答1: Spring Boot默认使用内置Tomcat作为Web服务器。如果需要配置Tomcat,可以在application.properties或application.yml文件中添加相关配置。 例如: server.port = 8080 # 端口号 server.context-path = /myapp # 上下文路径 server.tomcat.uri-encoding = UTF-8 # URI编码 更多配置请参考Spring Boot文档中的"Server Properties"部分。 ### 回答2: Spring Boot 是一个流行的 Java 开发框架,它内置Tomcat 作为默认的 Web 服务器,并支持自定义配置。 在 Spring Boot 中,我们可以通过 Maven 或 Gradle 工具构建和运行 Web 应用程序。 下面是 Spring Boot 内置 Tomcat 配置的详细内容: 1.端口配置内置 Tomcat配置文件中,我们可以配置应用程序的监听端口,例如,可以更改 Tomcat 默认端口 8080 为自定义的端口(比如 8999)。 这可以通过在 application.properties 或 application.yml 文件中添加以下行来完成: server.port=8999 2.上下文根配置 我们可以通过配置上下文根来指定应用程序的访问路径。 默认情况下,Spring Boot 使用 / 设置上下文根。 我们可以通过在 application.properties 文件中添加以下行来更改上下文根: server.servlet.context-path=/example 在上述配置中,我们将上下文根设置为 /example。 3.启动 Spring Boot 在启动时会自动加载内置Tomcat。 应用程序在启动时创建 Tomcat 容器,并在其中部署 WAR 文件。 默认情况下,Spring Boot 在应用程序的类路径中查找名为 “ROOT.war” 的 WAR 文件来部署应用程序。 但是,我们可以通过以下配置向 Spring Boot 更改 WAR 文件的位置: spring.boot.admin.context-path=/example 在上面的配置中,我们将 WAR 文件的位置更改为 /example 目录下。 4.连接器配置 我们可以使用 Spring Boot 的默认连接器或自定义连接器来配置 Tomcat。 默认情况下,Spring Boot 使用 HTTP 连接器。 我们可以通过以下配置添加 SSL 连接器: server.ssl.key-store: classpath:/ssl/keystore.jks server.ssl.key-store-password: password server.ssl.key-password: password 上述配置中,我们将使用位于类路径下的 keystore.jks 作为密钥库,并使用密码 password。 5.其他配置 除了上述配置,还有许多其他方式可以配置 Spring Boot 内置Tomcat。 例如,我们可以使用指定容器的 JVM 参数、文件上传大小、会话超时和 Cookie 选等。 我们可以在 Spring Boot 的官方文档中查看详细的配置。 总之,Spring Boot 内置 Tomcat配置提供了许多选,可以用来自定义应用程序的细节。 只需简单地配置一下,即可轻松地启动和访问我们的 Web 应用程序。 ### 回答3: SpringBoot是一种开源框架,它提供了一些必须的功能来快速构建应用程序。SpringBoot内置Tomcat作为Web服务器,这意味着我们可以轻松地将Tomcat作为应用程序容器运行。 SpringBoot会自动配置Tomcat。我们只需要在应用程序中添加SpringBoot Web依赖,然后就可以启动嵌入式Tomcat服务器了。Tomcat默认绑定在8080端口上。我们可以通过application.properties或application.yml文件来修改Tomcat配置。以下是一些常见的配置: 1. 修改端口号 在application.properties文件中,我们可以通过设置server.port属性来修改Tomcat监听的端口号。例如,如果我们想将Tomcat绑定到8081端口上,则可以在application.properties文件中添加以下代码: server.port=8081 2. 更改上下文路径 默认情况下,SpringBoot应用程序的上下文路径为“/”,也就是根路径。我们可以通过修改server.contextPath属性来更改上下文路径。例如,如果我们想将上下文路径更改为“/myapp”,可以在application.properties文件中添加以下代码: server.contextPath=/myapp 3. 修改最大连接数 在application.properties文件中,我们可以通过设置server.tomcat.max-connections属性来修改Tomcat服务器的最大连接数。例如,如果我们想将最大连接数更改为1000个,则可以在application.properties文件中添加以下代码: server.tomcat.max-connections=1000 总之,SpringBoot内置Tomcat可以减少我们的应用程序开发和配置工作。我们只需要添加某些依赖关系和配置,就可以启动嵌入式Tomcat服务器,并且可以轻松地修改Tomcat配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值