Nginx-based static Web page deployment

Source: Internet
Author: User

Background:

A sequence of HTML pages needs to be deployed

Nginx-based deployment:

The Openresty-based Nginx configuration is used in this paper.
Simply configure Nginx configuration files so that when you start Nginx, you can enable these configurations to allow click-to-jump access for well-written HTML pages. And this is the focus of this article.

Configuration Mode 1:

Nginx configuration system consists of a master configuration file and some other auxiliary configuration files. These profiles are plain text files, and generally we just need to configure the Master profile. /usr/local/openresty/nginx/confThe following configuration files are 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 1024x768;
}

http{
Resolver 10.1.16.10;
includeMime.types;
Default_typeApplication/octet-stream;

Log_formatMain'$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 -;
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 -;
Tcp_nodelay on;

gzip on;
Gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
Gzip_comp_level 2;
Gzip_typesText/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_dictCache100m;
Lua_code_cache on;
lua_shared_dictLyrics_monitor_cnt1024K;

Server{
Listen 8081;# Listen for 8081 ports on all IP on this machine
server_name_;# Domain Name: www.example.com Here "_" represents get match all
Root/home/liujiepeng/workspace/html/etc/resource/html/;# site root directory
Indexhome.html;
}
}

Create a directory, for example:/home/liujiepeng/workspace/html/etc/resource/html/and then in this HTML folder you can place the static page file you need to deploy, such as HTML I have Google, Baidu, Liujiepeng these three folders, where the server field is configured as follows:

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

The static page filenames below each folder are home.html. This configuration, for example, when you visit the www.example.com/google/, Nginx will go to the root specified directory under the Google folder to find home.html and return the Google page, similarly, access to www.example.c om/baidu/, will find the Baidu folder under the home.html and the Baidu page back.

In the Google, Baidu, Liujiepeng folder sibling directory, and then add your domain name home home.html, access to www.example.com will return.

The only drawback is that access to the domain name in the end of Www.showzeng.cn/zhihu will automatically add/, in the browser press F12 debugging will find Www.showzeng.cn/zhihu as 301 status code, because Index.html is in zhihu/ folder, so you will be redirected to www.showzeng.cn/zhihu/during the search

Configuration Mode 2:

It is important to note that the server context is in the HTTP context.

server              {
Listen 8081 ; # monitor 8081 port on all IP on this machine
server_name _; # domain name: www.example.com Here "_" represents get match all
root/home/filename/; # site root

Location/{# can have multiple locations for configuring routing addresses
TR Y_files index . html =404 ;
}
}

The root field here is best written in the outside of the location field to prevent the failure to load CSS, JS. Because the CSS, JS loading is not automatic, Nginx cannot execute, need additional configuration to return resources, so, for the deployment of static pages, this is the most convenient. The
here is a further explanation of root, such as having/home/liujiepeng/workspace/html/etc/resource/html/on the server, with index.html files and css/and img/, root/h ome/liujiepeng/workspace/html/etc/resource/html/This configuration statement loads the specified server when the resource is/home/liujiepeng/workspace/html/etc/ Search under resource/html/.
Second, the location after the matching of a variety of different types of matching priority is also different. Here is an example of an exact match:

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

location = /zhihu {
break;
index.html =404;
}
}

此时,访问 www.example.com/liujiepeng 就会加载 zhihu.html 出来了。由于 location 的精确匹配,只有访问 www.example.com/liujiepeng 这个路由时才会正确响应,而且此时要通过 rewrite 正则匹配,把 /zhihu 解析替换成原来的 / 。关于更多 location 字段用法,可以在文章最后给出的参考资料中查看。

参考:
http://showzeng.itscoder.com/nginx/2016/10/03/use-nginx-to-deploy-static-pages-easily.html

Nginx-based static Web page deployment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.