Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of static Web Page deployment based on nginx

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Background:

A sequence of html pages needs to be deployed

Nginx-based deployment:

The openresty-based nginx configuration used in this article.

Simply configure the configuration file of Nginx so that when you start Nginx, you can enable these configurations to achieve click-and-jump access to written html pages. And the focus of this article is also here.

Configuration mode 1:

The configuration system of Nginx consists of a main configuration file and other auxiliary configuration files. These configuration files are plain text files. In general, we only need to configure the main configuration file. The configuration file under / usr/local/openresty/nginx/conf is modified as follows:

Configuration Information:

# user nobody;worker_processes 1 the errorists 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\ tasking httpboxes refererer'\ t$http_user_agent\ tasking httpflows referrals for'\ t$host\ t$request_time\ t$upstream_addr\ t$upstream_status\ tstreaming responsetimeframes; 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 on port 8081 server_name _; # domain name: www.example.com where "_" means to get matching all root / home/liujiepeng/workspace/html/etc/resource/html/; # site root index Home.html;}}

Create a directory, such as / home/liujiepeng/workspace/html/etc/resource/html/, and then place the static page files you need to deploy under this html folder. For example, I have google, baidu and liujiepeng folders under html, in which the server field is configured as follows:

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

The static page file name under each folder here is Home.html. In this way, for example, when you visit www.example.com/google/, nginx will go to the google folder under the directory specified by root to find Home.html and return the google page. Similarly, when you visit www.example.com/baidu/, you will find the Home.html under the baidu folder and return the baidu page.

In the same directory of the google, baidu and liujiepeng folders, when you add your domain name home page Home.html, it will be returned when you visit www.example.com.

The only drawback here is that the / is automatically added at the end of the www.showzeng.cn/zhihu in the access domain name. If you press F12 to debug in the browser, you will find that www.showzeng.cn/zhihu is 301 status code. Because index.html is under the zhihu/ folder, it will be redirected to www.showzeng.cn/zhihu/ during the search process.

Configuration mode 2:

What needs to be noticed here is the server context in the http context.

Server {listen 8081; # listen on port 8081 server_name _; # domain name: www.example.com where "_" means to get matching all root / home/filename/; # site root directory location / {# multiple location can be used to configure the routing address try_files index.html = 404;}}

The root field here is best written outside the location field to prevent css and js from being loaded. Because the loading of css and js is not automatic, nginx cannot be executed, and additional configuration is required to return resources, so this is most convenient for static page deployment.

Root is further explained here. For example, there are index.html files and css/ and img/ under / home/liujiepeng/workspace/html/etc/resource/html/, on the server. The configuration statement root / home/liujiepeng/workspace/html/etc/resource/html/ will specify that the server is looking under / home/liujiepeng/workspace/html/etc/resource/html/ when loading resources.

Secondly, there are many kinds of matching after location, and the priority of all kinds of matching methods are 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, accessing www.example.com/liujiepeng will load the zhihu.html. Because of the exact match of location, the correct response will only be obtained when the route www.example.com/liujiepeng is accessed, and the / zhihu parsing will be replaced with the original / through rewrite regular matching. For more information on the use of the location field, you can see it in the Resources given at the end of the article.

Reference: https://www.jb51.net/article/141340.htm

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report