Explanation:
The recent project is a Webgl project, including the front end (Webgl) + database. Here, I use Node.js as the middleware to encapsulate the GET and POST interfaces to operate the database. Another point is that the server only opens one port to the outside world. If you want to process the logic of multiple interfaces through one port (Webgl uses port 80, and Node.JS listens on port 3000), here is the Nginx reverse proxy, which has not been used for many things, but also encountered Lots of questions.
Directly use node.js to encapsulate the interface API to test GEt and POST requests without any problems. After adding Nginx, it is found that the form data of the POST request is null after being sent to the server, indicating that the front-end logic should be no problem. The main solution is in the Nginx configuration Start with the front-end test code first:
//测试
public void TestData(WWWForm form, Action<bool> callback)
{
StartCoroutine(TestData_IE(form, callback));
}
//使用了Nginx:http://1.117.228.127:80/updateData
//不使用Nginx: http://1.117.228.127:3000/updateData
IEnumerator TestData_IE(WWWForm form,Action<bool> callback)
{
using (UnityWebRequest request=UnityWebRequest.Post("http://1.117.228.127:80/updateData", form))
{
yield return request.SendWebRequest();
if (!string.IsNullOrEmpty(request.error))
Debug.LogError(request.error);
Debug.Log(request.downloadHandler.text);
if (request.downloadHandler.text=="true")
{
callback(true);
}
else
{
callback(false);
}
}
}
}
The server
is related to the server and Windows Server 2012R2
the database. MySQL
Here I use the PHPStudy integrated environment software, which is more friendly to those who are not familiar with the back end like me. Here is to start the Nginx service.
Then modify its corresponding configuration file. Click to open the Nginx root directory, find the conf folder and enter to find the nginx.conf file, which is the Nginx configuration file.
Start to nginx.conf
configure, the server part in the code is the related configuration added.
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 40960;
}
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 127.0.0.1;
#charset koi8-r;
#access_log logs/host.access.log main;
location /Test/ {
proxy_pass http://127.0.0.1:3000/Test/;
}
location /updateData{
proxy_pass http://127.0.0.1:3000/updateData;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
add_header 'Access-Control-Allow-Origin' $http_origin;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,web-token,app-token,Authorization,Accept,Origin,Keep-Alive,User-Agent,X-Mx-ReqToken,X-Data-Type,X-Auth-Token,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# .unityweb类型文件添加gzip压缩头
# wasm使用gzip压缩,设置MIME type为application/wasm wasm
location ~ \.js.gz$ {
add_header Content-Encoding gzip;
default_type application/javascript;
}
location ~ \.wasm.gz$ {
add_header Content-Encoding gzip;
default_type application/wasm;
}
location ~ \.data.gz$ {
add_header Content-Encoding gzip;
default_type application/octet-stream;
}
location ~ \.symbols.json.gz$ {
add_header Content-Encoding gzip;
default_type application/octet-stream;
}
## br support
location ~ \.wasm.br$ {
add_header Content-Encoding br;
default_type application/wasm;
}
location ~ \.js.br$ {
add_header Content-Encoding br;
default_type application/javascript;
}
location ~ \.data.br$ {
add_header Content-Encoding br;
default_type application/octet-stream;
}
location ~ \.symbols.json.br$ {
add_header Content-Encoding br;
default_type application/octet-stream;
}
}
# 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;
# }
#}
#include vhosts.conf;
map $time_iso8601 $logdate {
'~^(?<ymd>\\d{4}-\\d{2}-\\d{2})' $ymd;
default 'date-not-found';
}
include vhosts/*.conf;
# 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;
# }
#}
client_max_body_size 50m;
client_body_buffer_size 60k;
client_body_timeout 60;
client_header_buffer_size 64k;
client_header_timeout 60;
error_page 400 /error/400.html;
error_page 403 /error/403.html;
error_page 404 /error/404.html;
error_page 500 /error/500.html;
error_page 501 /error/501.html;
error_page 502 /error/502.html;
error_page 503 /error/503.html;
error_page 504 /error/504.html;
error_page 505 /error/505.html;
error_page 506 /error/506.html;
error_page 507 /error/507.html;
error_page 509 /error/509.html;
error_page 510 /error/510.html;
keepalive_requests 100;
large_client_header_buffers 4 64k;
reset_timedout_connection on;
send_timeout 60;
sendfile_max_chunk 512k;
server_names_hash_bucket_size 256;
}
worker_rlimit_nofile 100000;
The configuration here is that the outside world listens to port 80, and external users access related requests on port 80. For example http://1.117.228.127:80/Test
, Nginx will automatically turn to the GET request http://127.0.0.1:3000/Test, because the initial requirement is to open only one port to the outside world, here Realized opening a port 80 to the outside world, and then performed reverse proxy.
Problems encountered:
Nginx反向代理对POST请求的表单数据,服务器打印测试一直为null
This is a simple configuration at the beginning
location /Test/ {
proxy_pass http://127.0.0.1:3000/Test/;
}
location /updateData/{
proxy_pass http://127.0.0.1:3000/updateData;
}
In the modified configuration, two points have been modified here, and then the POST is successful.
First, after location /updateData /
, remove
the second, add some settings
location /Test/ {
proxy_pass http://127.0.0.1:3000/Test/;
}
location /updateData{
proxy_pass http://127.0.0.1:3000/updateData;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
proxy_redirect
This command is used to modify the Location header field and "refresh" header field in the response header returned by the proxy server.
proxy_set_header
It is used to redefine the request header sent to the backend server. When the Host is set to $http_host, the value of the request header will not be changed.