nginx配置轉發(普通html檔案)
阿新 • • 發佈:2020-09-07
很多時候我們都會遇到跨域問題,現在最普遍的解決問題的辦法就是nginx,不會安裝的請看我上一篇部落格,nginx的簡單使用,本篇主要介紹本地的html檔案如何訪問後端電腦的介面檔案,或者訪問線上的介面地址檔案
nginx配置
本地配置
location / {
root E:/nginxtext/index; //本地的專案地址
index index.html index.htm; //專案初始檔案
}
轉發配置
location /api/ { proxy_pass http://47.104.17.68:9091/hrsys/; //轉發到線上介面 proxy_set_header: Host $host; }
配置預覽
頁面ajax請求
完整配置
#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 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root E:/nginxtext/index; index index.html index.htm; } location /api/ { proxy_pass http://47.104.17.68:9091/hrsys/; proxy_set_header: Host $host; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # 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; # } #} }
簡單的html頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> 簡單的測試一下nginx </body> </html> <!-- 引入jq --> <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script> getNginx() function getNginx() { let data = { "passWord": "123456", "userAccount": "123456" } $.ajax({ url: "/api/login", //請求的url地址 相當於http://47.104.17.68:9091/hrsys/queryUser dataType: "json", //返回格式為json async: true, //請求是否非同步,預設為非同步,這也是ajax重要特性 contentType: "application/json", //傳遞的格式為json格式 data:JSON.stringify(data), //引數值 type: "POST", //請求方式 beforeSend: function () { //請求前的處理 }, success: function (req) { //請求成功時處理 console.log(req) }, complete: function () { //請求完成的處理 }, error: function () { //請求出錯處理 } }); } </script>
頁面請求展示