隐藏

Nginx解决跨域、大文件、负载均衡和域名管理的问题(亲测真实有效)

发布:2022/6/30 23:15:40作者:管理员 来源:本站 浏览次数:1070

Nginx

Nginx在解决跨域、负载均衡和域名管理的问题上,有非常好的作用!值得应用。
在工作和学习中,前后端交互的时候,时常会需要做跨域的事情,这个时候,有两种方式,一种是服务端代码程序中去解决跨域,这是一种比较愚蠢的方式,推荐使用Nginx进行跨域的操作。


一、安装Nginx

这边安装Nginx有一个写的比较好的,我也是采用这个方式!
安装Nginx

二、重要的目录

root@ubuntu:/etc/nginx# ls conf.d          koi-utf     nginx.conf    sites-available  uwsgi_params
fastcgi.conf    koi-win     proxy_params  sites-enabled    win-utf
fastcgi_params  mime.types  scgi_params   snippets
root@ubuntu:/etc/nginx# 

这边的nginx.conf 这个是配置文件,在每次修改后,需要重启才会生效。

三、nginx里面的sites-available和sites-enabled有什么区别

If you are coming from Apache, the "sites-available" and "sites-enabled" directories will be familiar.

  These directories are used to define configurations for your websites. 
Files are generally created in the "sites-available" directory, and then symbolically linked to the "sites-enabled" directory when they are ready to go live.

...

In the "nginx.conf" file, we can see that the end of the "http" block has: include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

sites-available是存放当前的server配置, 在这里修改
sites-enabled是激活并使用的server配置(从sites_available的文件创建快捷方式到sites-enabled)

四、解决监听域名冲突(不生效)的问题。

默认情况下,对serve_name的配置应该实在 sites-available 目录下,Nginx默认加载的serve_name就是这里面的值。如果你不想要 sites-available 目录下的 defeat 文件生效。那么直接将此文件改个名字,保持这边没有文件。那么这个时候,Nginx就会去加载 nginx.conf 文件了。
Nginx解决跨域、大文件、负载均衡和域名管理的问题(亲测真实有效)

上面的 _ 符号表示默认的监听本机器的IP。

五、那么在 nginx.conf 中怎么配置合适呢?

只需要在 Http{} 中,按照如下配置即可

server

   {
    listen 80;
    server_name _;

    location ~ ^/front/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,cookietoken,cookieuid'; if ($request_method = 'OPTIONS') { return 200;
        }

        proxy_pass http://47.98.233.15:8099; proxy_redirect          off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location ~ ^/back/ {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
        add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization,cookietoken,cookieuid,lang'; if ($request_method = 'OPTIONS') { return 200;
        }

        proxy_pass http://47.98.233.15:8087; proxy_redirect          off;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

六、然后按照如下的访问即可

Nginx解决跨域、大文件、负载均衡和域名管理的问题(亲测真实有效)

七、如果你上传的文件比较大

需要增加以下配置,30m根据你的需要写,100m等都可以。

server

   {
    listen 80;
    server_name _;
    client_max_body_size 20m; ...... }

好啦,祝你学习和工作顺利!

本页地址:http://dude6.com/article/268510.html


免责申明:文章和图片全部来源于公开网络,如有侵权,请通知删除 server@dude6.com