Using Nginx server to publish front page

Keywords: Nginx PHP less JSON

As an http server, Nginx can be used as a web page static server. Before deploying static resources, let's learn about the Nginx configuration file

user nobody;
#Start the process, usually set equal to the number of CPUs
worker_processes  1;
 
#Global error log and PID file
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
 
#pid        logs/nginx.pid;
 
#Working mode and maximum number of connections
events {
    #epoll is a way of multiplexing IO(I/O Multiplexing),
    #Only used in Linux 2.6 or above kernel, can greatly improve the performance of nginx
    use   epoll; 
 
    #Maximum number of concurrent links for a single background worker process    
    worker_connections  1024;
 
    # The total number of concurrent processes is the product of worker processes and worker connections
    # That is, max? Clients = worker? Processes * worker? Connections
    # Why max? Clients = worker? Processes * worker? Connections / 4 with reverse proxy set
    # Why the above reverse proxy should be divided by 4? It should be an empirical value
    # According to the above conditions, the maximum number of connections that a Nginx Server can handle under normal circumstances is: 4 * 8000 = 32000
    # The setting of the worker ﹣ connections value depends on the size of the physical memory
    # Because concurrency is constrained by IO, the value of Max clients must be less than the maximum number of files the system can open
    # The maximum number of files that the system can open is directly proportional to the memory size. Generally, the number of files that can be opened on a 1GB memory machine is about 100000
    # Let's see how many file handles can be opened by VPS with 360M memory:
    # $ cat /proc/sys/fs/file-max
    # Output 34336
    # 32000 < 34336, that is, the total number of concurrent connections is less than the total number of file handles that the system can open, so it is within the range that the operating system can bear
    # Therefore, the value of worker's connections should be set appropriately according to the number of worker's processes and the maximum number of files the system can open
    # Make the total number of concurrent files smaller than the maximum number of files that the operating system can open
    # In essence, it is configured according to the physical CPU and memory of the host
    # Of course, the total number of concurrency in theory may deviate from the actual amount, because the host has other working processes that need to consume system resources.
    # ulimit -SHn 65535
 
}
 
 
http {
    #Set the MIME type, which is defined by the mime.type file
    include    mime.types;
    default_type  application/octet-stream;
    #Format log
    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;
 
    #The sendfile instruction specifies whether nginx calls the sendfile function (zero copy mode) to output the file,
    #For normal applications, it must be set to on,
    #If it is used to download applications, such as disk IO heavy load applications, it can be set to off,
    #In order to balance the disk and network I/O processing speed, reduce the uptime of the system
    sendfile     on;
    #tcp_nopush     on;
 
    #Connection timeout
    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay     on;
 
    #Turn on gzip compression
    gzip  on;
    gzip_disable "MSIE [1-6].";
 
    #Set request buffer
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;
 
 
    #Set virtual host configuration
    server {
        #Listen for port 80
        listen    80;
        #Definition visit www.nginx.cn
        server_name  www.nginx.cn;
 
        #Define the default site root location for the server
        root html;
 
        #Set the access log of this virtual host
        access_log  logs/nginx.access.log  main;
 
        #Default request
        location / {
            
            #Define the name of the first page index file
            index index.php index.html index.htm;   
 
        }
 
        # Define error prompt page
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }
 
        #Static files, handled by nginx itself
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
            
            #The static file is not updated for 30 days after expiration. You can set it a little larger after expiration,
            #If you update frequently, you can set it a little smaller.
            expires 30d;
        }
 
        #All PHP script requests are forwarded to FastCGI for processing. Use FastCGI default configuration
        location ~ .php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
 
        #Access to. htxxx files is prohibited
            location ~ /.ht {
            deny all;
        }
 
    }
}

Configure Nginx/Tengine

Please make sure that your server has Nginx or Tengine installed (take Tengine for example in this article)

  • Copy the static resource file to the specified directory, such as / home/admin

  • Configure nginx-proxy.conf file

server {
        listen       8089;
        server_name  localhost;
        location /resource_static/ {
            root   /home/admin/;
        }

    }

The listening port configured in this paper is 8089, depending on the situation

The above configuration indicates that when inputting localhost: 8089 / resource ﹣ static /, it will access the / home / admin / resource ﹣ static / directory of the machine. Create a new file test.json under / home / admin / resource ﹣ static /, as shown below:

Enter in the browser:

localhost:8089/resource_static/test.json

Cross domain problem

Cross domain problems are often encountered, such as the following errors:

Access to Font at 'http://xxx:8089/resource_static/console/hello.ttf' from origin 'http://xxx:8089' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://xxx:8080' is therefore not allowed access.
  • resolvent:

If it is configured like this, there will still be problems

        location /resource_static/ {
            root   /home/admin/;
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }

Posted by condoug on Sun, 05 Apr 2020 01:51:52 -0700