nginx配置代理 , 代理vue前端项目

nginx代理vue项目, nginx的配置文件:

假如访问52ccn.com 代理到后面的vue项目
vue项目路径/www/website/red/admin

nginx ssl证书位置在这个文件夹下面/usr/local/nginx/conf/cert/

然后项目中接口请求统一使用/prod-api/

然后通过nginx代理到对应得http://localhost:9032/ 对应得后台服务接口

worker_processes  1;
  events {
              worker_connections  1024;
          }
http {
            include       mime.types;
            default_type  application/octet-stream;
            sendfile        on;
            keepalive_timeout  300;
            client_max_body_size 1000m;

    

        server {
            listen 80;
            server_name 52ccn.com; #需要将domain替换成证书绑定的域名。
            rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
            location / {
                index index.html index.htm;
            }
        }

       server {
        listen      443 ssl;
        #请求域名
        server_name  52ccn.com;
        #证书位置在这个文件夹下面/usr/local/nginx/conf/cert/
        ssl_certificate   /usr/local/nginx/conf/cert/7296768_test.52ccn.com.pem;
        ssl_certificate_key  /usr/local/nginx/conf/cert/7296768_test.52ccn.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        #请求域名然后显示admin下面的前台资源页面
         location / {
            root   /www/website/red/admin;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
            proxy_set_header Host $http_host;
        }
        #前台资源接口如果有/prod-api/ 开头的接口 走下面的代理9032服务器
        location /prod-api/{
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:9032/;


        }
        #前台资源接口如果有/dev-api/ 开头的接口 走下面的代理9032服务器   
        location /dev-api/{
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:9032/;

            
        }

 
        #前台资源接口如果有/ysy-api/ 开头的接口 走下面的https://open.ys7.com/服务器 
        location /ysy-api/ {
            proxy_pass https://open.ys7.com/;
        }
        #前台资源接口如果有/mino-api/ 开头的接口 走下面的代理9032服务器 
        location /mino-api/ {
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header REMOTE-HOST $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://localhost:9000/;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }	
}

HTTP访问自动跳转到HTTPS页面

server {
    listen 80;
    server_name 52ccn.com; #需要将domain替换成证书绑定的域名。
    rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。
    location / {
        index index.html index.htm;
    }
}

这样访问52ccn.com 就自动跳https://52ccn.com 上了

posted @ 2022-02-26 15:16  菜鸟辉哥  阅读(4244)  评论(0编辑  收藏  举报