nginx 部署 vue 项目代码注释说明

server {
    listen 80;
    server_name 127.0.0.1;  
	
    location / {
        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  $scheme;                    
        proxy_set_header Host               $http_host;
        proxy_set_header X-NginX-Proxy      true;
        proxy_set_header Upgrade            $http_upgrade;              
        proxy_set_header Connection         "upgrade";                  
        proxy_read_timeout                  5m;
        proxy_connect_timeout               5m;
        proxy_redirect                      off;
        client_max_body_size                400m;                         
        proxy_pass_header                   Server;                     
        proxy_pass_header                   X-Accel-Buffering;          
        server_tokens                       off;
        
        proxy_pass http://127.0.0.1:8080;
    }
    
    #解决vuejs应用在nginx非根目录下部署时访问404的问题
    ##location ^~/new {
    ##    alias E:/Code/System/trunk/System/frontEnd/System_new;
    ##    #index index.html;
    ##    try_files $uri $uri/ @rewrites; 
    ##}           
    ##location @rewrites {
    ##    rewrite ^/(new)/(.+)$ /$1/index.html last;
    ##}

    location ^~/mm/new {
        alias E:/Code/System/trunk/System/frontEnd/System;
  		##alias htmi/dist_new;
        #index index.html;
        try_files $uri $uri/ @rewrites_mm; 
    }           
    location @rewrites_mm {
        rewrite ^/(mm/new)/(.+)$ /$1/index.html last;
    }
    
    #跨域代理
	##location /api  {
    ##    root   html;
    ##    index  index.html index.htm;
    ##    
    ##    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  $scheme;                    
    ##    proxy_set_header Host               $http_host;
    ##    proxy_set_header X-NginX-Proxy      true;
    ##    proxy_set_header Upgrade            $http_upgrade;              
    ##    proxy_set_header Connection         "upgrade";                  
    ##    proxy_read_timeout                  5m;
    ##    proxy_connect_timeout               5m;
    ##    proxy_pass                          "http://127.0.0.1:8081/";   
    ##    proxy_redirect                      off;
    ##    client_max_body_size                4m;                         
    ##    proxy_pass_header                   Server;                     
    ##    proxy_pass_header                   X-Accel-Buffering;          
    ##    server_tokens                       off;
    ##    ##error_page 405 =200                 http://$host$request_uri;
	##}
	##location /bpi  {
    ##    root   html;
    ##    index  index.html index.htm;
    ##    
    ##    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  $scheme;                    
    ##    proxy_set_header Host               $http_host;
    ##    proxy_set_header X-NginX-Proxy      true;
    ##    proxy_set_header Upgrade            $http_upgrade;              
    ##    proxy_set_header Connection         "upgrade";                  
    ##    proxy_read_timeout                  5m;
    ##    proxy_connect_timeout               5m;
    ##    proxy_pass                          "http://127.0.0.1:8081/";   
    ##    proxy_redirect                      off;
    ##    client_max_body_size                4m;                         
    ##    proxy_pass_header                   Server;                     
    ##    proxy_pass_header                   X-Accel-Buffering;          
    ##    server_tokens                       off;
    ##    ##error_page 405 =200                 http://$host$request_uri;
	##}
    ##
    ##location /ws{
    ##    proxy_pass http://127.0.0.1:8282/;
    ##    proxy_http_version 1.1;
    ##    proxy_set_header Upgrade $http_upgrade;
    ##    proxy_set_header Connection "Upgrade";
    ##}
    ##
    ##location /brws{
    ##    proxy_pass http://127.0.0.1:7272/;
    ##    proxy_http_version 1.1;
    ##    proxy_set_header Upgrade $http_upgrade;
    ##    proxy_set_header Connection "Upgrade";
    ##}
        
    ##java开头
	location /java/api  {
        root   html;
        index  index.html index.htm;
        
        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  $scheme;                    
        proxy_set_header Host               $http_host;
        proxy_set_header X-NginX-Proxy      true;
        proxy_set_header Upgrade            $http_upgrade;              
        proxy_set_header Connection         "upgrade";                  
        proxy_read_timeout                  5m;
        proxy_connect_timeout               5m;
        proxy_pass                          "http://127.0.0.1:8081/";   
        proxy_redirect                      off;
        client_max_body_size                400m;                         
        proxy_pass_header                   Server;                     
        proxy_pass_header                   X-Accel-Buffering;          
        server_tokens                       off;
        ##error_page 405 =200                 http://$host$request_uri;
	}
    
    location /java/ws{
        proxy_pass http://127.0.0.1:8282/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
    
    location /java/brws{
        proxy_pass http://127.0.0.1:7272/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
}

nginx 配置修改,假设做了跨域代理。核心代码如下:

server {
        listen       1081;                    
        server_name  localhost;              

        location / {
            root   html/dist;                
            index  index.html index.htm;         
			try_files $uri $uri/ /index.html;
        }

		# 设置代理,解决接口跨域问题
        location /api {
			rewrite  ^/api(.*)$ /$1 break; 
			proxy_set_header x-forwarded-for $remote_addr;
            proxy_pass   http://127.0.0.1:8105;
        }
    }

 

你可能感兴趣的:(vue,nginx)