Nginx配置前后端分离的项目的配置文件nginx.conf以及Tomcat的配置文件server.xml

Nginx作为静态资源服务的性能要比tomcat好,所以我们将前端的静态资源放在Nginx中,而后端的请求则放在tomcat中。

一.Nginx配置文件nginx.conf

闲来无事,配个nginx玩玩,下面是nginx.conf配置前后端分离项目的配置文件:

只需要主意后台配置的位置location /mgspringboot-0.0.1-SNAPSHOT/就可以了,前台请求/MG/好理解

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

    proxy_http_version 1.1;
    proxy_set_header Connection "";

    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    

    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;	
    #配置一个代理即tomcat1服务器
    upstream tomcat_server1 {
      server ip:8089 max_fails=60 fail_timeout=10s;
    }
  
    server {
            listen       80; #首先是nginx的监听端口默认为80
            server_name  www.******.com; #这里是你需要访问的域名地址
	    #add_header 'Access-Control-Allow-Origin' '*';#这里是http 域名跨域,报错时候添加的请求头,这样写所有请求都会进来,会很不安全。
            #charset koi8-r;
            #access_log  logs/host.access.log  main;#这里是 日志文件的生成路径
            #root  /usr/myNginx/nginx-1.13.3/html/MG;
            #index index.html index.htm;
    
	#详细介绍location
        location / {
            #是监听的端口默认访问的地址,这里如果没有做tomcat的转发则会进入nginx的html目录下的index.html
            root  /usr/myNginx/nginx-1.13.3/html/MG;       		
	    #设置nginx 的默认显示页
            index  index.html index.htm;

        }
   location /mgspringboot-0.0.1-SNAPSHOT/ {
            proxy_pass  http://tomcat_server1/mgspringboot-0.0.1-SNAPSHOT/;
            proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
            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_redirect off;
            proxy_connect_timeout 300s;
            proxy_send_timeout 300s;
            proxy_read_timeout 300s;
            proxy_buffer_size 512k;
            proxy_buffers 32 512k;
            proxy_busy_buffers_size 512k;
            proxy_temp_file_write_size 512k;
            proxy_ignore_client_abort on;
        }

        #前端静态资源文件
	location /MG/ {
	    #此处为什么不加MG呢?
            root   /usr/myNginx/nginx-1.13.3/html;
	    #autoindex on;
        }
	#location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
	#	root   /usr/myNginx/nginx-1.13.3/html;
	#}

        #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的配置server.xml





  
  
  
  
  
  
  
  

  
  
    
    
  

  
  

    
    


    
   
    
    
    
   
    
    
    
    

    
    


    

    
    

      
      

      
      
        
        
      

      
        
	
        
	
        

        
        

      
    
  

 

你可能感兴趣的:(Java)