No1. Download and install nginx: http://nginx.org/en/download.html choose corresponding version download, download, unzip the installation package, visual interface operation, all the way choice is commonly, balabala, over.
Nginx start nginx start nginx See the official API for details
Conf /nginx.conf (for front-end, proxy interface is required, file is used locally)
Make a hypothesis: local file path (C: / Users/admin/Desktop/webProject) interface request (http://www.test.com/api)
1, configure local code proxy related code:
location ^~ / {
root C:/Users/admin/Desktop/webProject;
}Copy the code
2. Configure interface cross-domain proxy code:
location ^~ /apis { rewrite ^.+apis/? $/ (. *)The $1 break; Rewrite ($1) rewrite ($1) rewrite ($1) rewrite ($1) rewrite ($1) rewrite ($1)
add_header Access-Control-Allow-Origin *;
proxy_pass http://www.test.com/api;
}Copy the code
No4. Start the service. Double-click nginx.exe to restart the service for each configuration change. Browser address bar enter localhost: 80 or 127.0.0.1:80 or native IP: 80 can access to the C: / Users/admin/Desktop/webProject directory, However, you need to manually enter the index.html or xxx. HTML folder. If you see the page, it indicates that the configuration of local code service is successful. If all interface requests are normal, it indicates that the configuration of interface service is successful.
No5. Pay attention to the point
1. The code path/ajax request apis/XXX, yyy, implement proxy interface at http://www.test.com/api/xxx/yyy
2. The default nginx port is 80. Ensure that the port is not occupied.
3. If there is any doubt, please refer to the file under log logs/
No6. The disadvantages need to add more layers /apis in the front-end code request path or define their own, but write baseUrl, separate online and offline also can be solved quickly.
No7. All configuration (can be used directly, instead of 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;
# taskkill /F /IM nginx.exe /T
events {
worker_connections 1024;
}
http {
#include .. /v_hostconf/*.conf;
include mime.types;
default_type application/octet-stream;
access_log off;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
server_names_hash_bucket_size 64;
#gzip on;
server {
listen 80;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers X-Requested-With;
add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
location ^~ /apis {
rewrite ^.+apis/? (.*)$ /$1 break;
add_header Access-Control-Allow-Origin *;
proxy_pass http://www.test.com/api;
}
location ^~ / {
root C:/Users/admin/Desktop/webProjectn;
}
}
}