nginx的使用,反向代理解决跨域问题
本篇介绍的是在window环境下的使用
首先下载nginx。下载地址
下载安装之后就可以运行了
start nginx //运行nginx nginx -s reload //重启nginx,每次修改配置后都要重启
nginx -s stop //停止服务
主要是nginx的配置,下面是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 { client_max_body_size 200m;#上传文件大小 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; include D:/nginx/nginx-1.18.0/sliceconf/*.conf;#分片的配置文件
# # 9519-9529 # server { # listen 9519;#MobileGadgets # server_name localhost; # # api # location /apiurl/ { # proxy_pass http://localhost:49628/api/; # } # location /signalr { # #测试 # proxy_pass http://localhost:54398/api/signalrhub/mysendall/chatHub; # proxy_set_header Upgrade $http_upgrade; # proxy_set_header Connection upgrade; # proxy_http_version 1.1; # proxy_ssl_server_name on; # } # # 页面 # location / { # proxy_pass http://localhost:9529/; # } # error_page 500 502 503 504 /50x.html; # location = /50x.html { # root html; # } # } }
可以代理的端口可以全部写在此文件夹下,写多个server即可。也可以拆分配置
上面代码中的include指向拆分的配置文件,子配置文件如下
server { listen 8082; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main location /apiurl/ {#接口代理 proxy_pass http://196.0.0.1:4523/mock/88888/; } location / { # root web;#可以是nginx文件夹下的 root E:/项目文件/发布文件/Map_gy; index index.html index.htm; } #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; } }