Nginx主配置文件(nginx.conf)

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include				mime.types;
    default_type		application/octet-stream;
    sendfile        	on;
    keepalive_timeout	65;
    gzip				on;

    server {
        listen			80;
        server_name		localhost;
		charset 		utf-8;#字符集,可以处理中文乱码
        location / {
            root		D:\xxxxx\xxxxx\xxxx\xxxx;
            index		index.html index.htm;
        }

		location /t {
	    	alias		D:\xxx\xxx\xxx\xxx\xx\xx;
		    index		index.html index.htm;
		}

		#前端代码部署(浏览器访问地址:http://127.0.0.1/nyjPC)
		location /nyjPC {
	    	alias		D:\xx\xx\xx\xx;
	    	index		index.html index.htm;
		}
	
		#后台springboot接口服务代理
		location /api {
			proxy_pass			http://127.0.0.1:8090/;#后端部署上线后的地址,注意端口后面的'/'要加上,不然会404
			proxy_set_header	X-Real-IP $remote_addr;
			proxy_set_header	X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header	Host $http_host;
			proxy_set_header	X-NginX-Proxy true;
			proxy_redirect		off;
        }​​​​
		
		#location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff|html|txt|pdf|) {
		#	root /usr/local/nginx/html/;	#所有静态文件直接读取硬盘
		#   expires 30d;					#缓存30天
		#}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root				html;
        }
    }
}

Vue项目配置文件(vue.config.js)

module.exports={
      publicPath:'./',
      //publicPath: process.env.NODE_ENV === "production" ? "./" : "/",
      //outputDir: process.env.NODE_ENV === "development" ? 'devdist' : 'dist',
      productionSourceMap: false,  // 生产环境是否生成 sourceMap 文件
      devServer:{
            host:'localhost',
            port:8080,
            host:'0.0.0.0',
            proxy:{
                  "/api":{//此处"api"与nginx中的/api一致,只是这里是开发环境用的,避免后期改动
                        ws:false,
                        target:'http://localhost:8090',//该地址是后端可访问地址
                        changeOrigin:false,
                        pathRewrite:{
                              '^/api':''
                        }
                  }
            }
      }
}

前端请求实例

//请求url中的'/api'就是vue.config.js中的代理的'/api',打包上线后就是Nginx主配置文件中的'/api'
axios.post("/api/users/login", {
	data:{
		userinfo:this.loginForm.username,
		password:this.loginForm.password
	},
	headers:{
		'Content-Type': 'application/json'
	}
}).then(resp=>{
	console.log(resp);
});

补充

如果SpringBoot整合了SpringSecurity可能会报错,内容如下:

2020-05-02 23:25:44.216 ERROR 17620 --- [nio-8082-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
	org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the URL contained a potentially malicious String "//"
	at org.springframework.security.web.firewall.StrictHttpFirewall.rejectedBlacklistedUrls(StrictHttpFirewall.java:369) ~[spring-security-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.security.web.firewall.StrictHttpFirewall.getFirewalledRequest(StrictHttpFirewall.java:336) ~[spring-security-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:194) ~[spring-security-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178) ~[spring-security-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_131]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_131]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]

内容倒是不少,但就是第一行的异常消息有效,大概意思的是:请求被拒绝异常:请求被拒绝,因为URL包含潜在的恶意字符串“/”,这个异常是SpringSecurity防火墙抛出来的,这个和安全有关嘛,所以可想而知了,解决办法就是在继承了WebSecurityConfigurerAdapterSpringSecurity配置类中注入一个Bean,内容如下:

//要导的包
import org.springframework.security.web.firewall.DefaultHttpFirewall;
import org.springframework.security.web.firewall.HttpFirewall;
@Bean
public HttpFirewall httpFirewall() {
	return new DefaultHttpFirewall();
}

以上内容已经过实测

Logo

前往低代码交流专区

更多推荐