Implementation of Nginx Configuration and Access to Local Static Resources in Mac Environment

  • 2021-09-05 01:22:02
  • OfStack

Local development sometimes needs to debug static file resources, which cannot be accessed directly. It can be done by configuring local Nginx service. By the way, record the configuration steps of Nginx

Installation


<!-- Pass  Brew  Installation:  -->
brew install nginx
<!-- Start:  -->
brew services start nginx
<!-- View the configuration:  -->
cat usr/local/etc/nginx/nginx.conf
<!-- Edit configuration:  -->
vi usr/local/etc/nginx/nginx.conf

Nginx command:


<!-- Start: -->
nginx

<!-- Stop / Restart -->
nginx -s stop/start/restart

Configuration file

File address: usr/local/etc/nginx/nginx. conf


#  Configured here as root owner To access root Static file of, otherwise it will be reported 403
user root owner;
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 {
    #  Listening port 
    listen    8080;
    #  Binding domain name 
    server_name local.XXX.com;

    #charset koi8-r;

    #access_log logs/host.access.log main;
    
    # File path and entry file 
    location / {
      root  /usr/local/var/www;
      index index.html index.htm;
    }
    
    #  Interface resource 1
    location /XXXapi/ {
      proxy_pass https://api.XXX.com; 
    }
    #  Interface resource 2
    location /apiXXX/ {
      proxy_pass https://api.XXX.com; 
    }

    #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;
    }
  }

  include servers/*;
}

Configuration steps

Installing Nginx Bind HOST via SwitchHost (127.0. 0.1 local. XXX. com) Configure ports and domain names

#  Listening port 
listen    8080;
#  Binding domain name 
server_name local.XXX.com;
 Specify the entry file and static file path 
# File path and entry file     
location / {      
 root  /usr/local/var/www;      
 index index.html index.htm;    
}
 If there are extra API Resources, through proxy_pass Bind the corresponding API Resource address 
#  Interface resource 1
location /XXXapi/ {
  proxy_pass https://api.XXX.com; 
}
#  Interface resource 2
location /apiXXX/ {
  proxy_pass https://api.XXX.com; 
}
Place static files in the file path of Nginx configuration
DONE, you can open static website resources and access them locally through the corresponding HOST

Related articles: