架构Nginx

nginx 反向代理解决前后台跨域

2021-12-20  本文已影响0人  暴躁程序员

登录依赖cookie,且cookie是跨域不共享的,如果是前后台分离开发,肯定存在跨域的情况,有时需要在服务器上同时启动前后台服务,然后使用nginx服务器配置反向代理来解决前后台跨域问题
参考:https://www.runoob.com/linux/nginx-install-setup.html

步骤

  1. 下载:https://nginx.org/en/download.html (下载速度很快,压缩包)
  2. 解压到指定位置,比如C盘根目录,并重命名为nginx
  3. 使用vscode打开 windows:C:\nginx\conf\nginx.conf
#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;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       9090;
        server_name  localhost;
        location / {
            proxy_pass http://localhost:9001;
        }
        location /dev-api/ {
            proxy_pass http://localhost:9000;
            proxy_set_header Host $host;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       3030;
        server_name  localhost;
        location / {
            proxy_pass http://localhost:3001;
        }
        location /api/ {
            proxy_pass http://localhost:3000;
            proxy_set_header Host $host;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }


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

}
  1. 保存测试
    用vscode或者win+R cmd 打开windows:C:\nginx
    输入:start nginx
    然后:nginx -t 检测是否成功启动,出现以下字样,代表nginx启动成功
    nginx: the configuration file C:\nginx/conf/nginx.conf syntax is ok
    nginx: configuration file C:\nginx/conf/nginx.conf test is successful
  2. 跨域测试
    浏览器输入:http://localhost:9090/index.html (此端口是监听端口,非前后端服务端口)
    打开页面后,前端服务可以正常访问接口,证明nginx跨域代理成功
  3. nginx 常用命令
    启动:start nginx 或者 nginx
    重启:nginx -s reload
    停止:nginx -s stop
    检测是否成功:nginx -t
上一篇下一篇

猜你喜欢

热点阅读