Implementation of static web page deployment based on nginx

  • 2020-05-14 06:08:36
  • OfStack

Background:

1 sequence of html pages to be deployed

Deployment based on nginx:

This article USES the openresty-based nginx configuration.

Simply configure the Nginx configuration files so that they can be enabled when Nginx is launched to enable click-to-jump access to the html page you have written. And this is the focus of this paper.

Configuration mode 1:

The Nginx configuration system consists of a master configuration file and some other auxiliary configuration files. These configuration files are all plain text files, like 1, and we just need to configure the main configuration file 。/usr/local/openresty/nginx/conf The following configuration file is modified as follows:

Configuration information:


#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 {
  resolver 10.1.16.10;
  include    mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr\t$remote_user\t[$time_local]\t$request '
    '\t$status\t$body_bytes_sent\t$http_referer'
    '\t$http_user_agent\t$http_x_forwarded_for'
    '\t$host\t$request_time\t$upstream_addr\t$upstream_status\t$upstream_response_time';

  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 30m;

  sendfile on;
  tcp_nopush   on;
  log_subrequest on;

  keepalive_timeout 60;
  tcp_nodelay on;

  gzip on;
  gzip_min_length 1k;
  gzip_buffers   4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types    text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

  lua_package_cpath 'lib/?.so;tcp/lib/?.so;/data1/htdocs/lua_v2/lib/*/?.so;;';
  lua_shared_dict cache 100m;
  lua_code_cache on;
  lua_shared_dict lyrics_monitor_cnt 1024K;

  server {
  listen 8081;       #  Listen to all  ip  On the  8081  port 
  server_name _;      #  Domain name: www.example.com  Here,  "_"  Represents fetch match all 
  root /home/liujiepeng/workspace/html/etc/resource/html/; #  Site root directory 
  index Home.html;
  }
}

Create a directory, for example: / home/liujiepeng/workspace/html/etc/resource html/then you need to deploy in this html folder can be placed in the static page file, such as I have google under html, baidu, liujiepeng the three folders, including server field configuration is as follows:


server {
    listen 80;
    server_name _;
    root /home/liujiepeng/workspace/html/etc/resource/html/;
    index Home.html;
}

The name of the static page file under each folder is Home.html. . Such a configuration, for example, when you visit www example. com google /, nginx will go root google under the specified directory folder to find Home. html google page back and, by the same token, the access www. example. com baidu /, will seek to baidu folder Home. html and put baidu page back.

When you add your home page, Home.html, to the google, baidu, liujiepeng equivalent, it will return when you visit www.example.com.

Here only one fly in the ointment is that access to the domain name in www. showzeng. cn/zhihu automatically add/at the end, in the browser by F12 debugging will find www. showzeng. cn/zhihu for 301 status code, because index. html zhihu/folder, so in the search process will be redirected to www. showzeng. cn/zhihu /

Configuration mode 2:

The important thing to note here is the server context in the http context.


server {
    listen 8081;       #  Listen to all  ip  On the  8081  port 
    server_name _;      #  Domain name: www.example.com  Here,  "_"  Represents fetch match all 
    root /home/filename/;  #  Site root directory 

    location / {       #  Can have multiple  location  Used to configure the routing address 
      try_files index.html =404;
    }
}

The root field here is best written outside the location field, in case the css and js cannot be loaded. Since the loading of css and js is not automatic, nginx cannot be performed and requires additional configuration to return resources, so this is most convenient for static page deployment.

Make further explanation on root here, for example, on the server/home liujiepeng/workspace/html/etc/resource html /, which have index html files and css and img /, root home/liujiepeng/workspace/html/etc/resource/html/this configuration statements will specify the server load resources is in/home/liujiepeng/workspace/html/etc/resource/html/lookup.

Secondly, there are many kinds of matches after location, and the priority of all kinds of matches is also different. Here is an example of an exact match:


server {
    listen 80;        
    server_name _;      
    root /home/zhihu/;  

    location = /zhihu {
      rewrite ^/.* / break;
      try_files index.html =404;
    }
}

At this point, the access www. example. com/liujiepeng will load zhihu. html came out. Due to the exact match location, only visit www. example. com/liujiepeng this routing will correct response, and at this time to pass rewrite regular match, put/zhihu resolution to replace the original. For more information on the use of the location field, see resources at the end of this article.

Reference: / / www. ofstack. com article / 141340. htm


Related articles: