As an experienced website operations expert, I am happy to elaborate in detail on the correct configuration method of the Nginx running directory when deploying AnQiCMS using Baota panel, and deeply analyze the principles behind it, ensuring that you not only know what it is, but also why it is so.
What should be the running directory for Nginx be configured to when deploying AnQiCMS via the Baota panel? Deep analysis and practice
When deploying applications such as AnQiCMS developed in Go language using the Baota panel, many operators may be accustomed to directly pointing the Nginx running directory to the root directory of the project.However, for AnQiCMS, the running directory of Nginx has specific **practices, which are crucial for the performance, security, and normal operation of the website.
答案揭晓:Nginx的运行目录应该配置到您的AnQiCMS项目目录下的Englishpublicfolder.
现在,让我们来详细了解一下为什么是EnglishpublicFolder and how to configure it on the Baota panel.
Understand the collaboration mode between AnQiCMS and Nginx
AnQiCMS is an enterprise-level content management system developed based on the Go language, with core features of high performance and simple deployment.Differing from traditional PHP applications (such as WordPress), Go applications usually include an efficient web server that can directly handle HTTP requests.Reverse Proxy (Reverse Proxy)andStatic File Server (Static File Server).
- reverse proxyEnglish:Nginx will receive all user requests and forward dynamic content requests to the Go application running within AnQiCMS (usually listening on port 8001).
- Static Resource ServiceAnQiCMS's CSS, JavaScript, images, and other static files, stored by default in the project directory.
publicFolder.To improve the website's loading speed and reduce the pressure on the backend Go application, we usually let Nginx directly handle the requests for these static files instead of forwarding them to the Go application each time.Nginx excels in handling static files, with efficiency far higher than having the backend application distribute them.
Based on such a collaboration mode, the Nginx running directory is set topublicfolder, and combined with specific rewrite rules, to achieve ** performance and features.
The practice of Nginx configuration in Baota Panel
When you create a site and configure Nginx for AnQiCMS in the Baota Panel, the process is roughly as follows:
Firstly, you need to unzip the AnQiCMS installation package to a directory on the server, for example/www/wwwroot/yourdomain.com. In this directory, you will see a file namedpublic.
Next, when creating a new website in the Baota panel, you can choose "Static" or not create a database (because AnQiCMS will connect to the database automatically). The key is the Nginx configuration:
Set Website Directory In the "Website Directory" tab of the website settings, you need to set the "Running Directory"precisely to the AnQiCMS project directory under your
publicFolder. For example, if the root directory of your AnQiCMS project is/www/wwwroot/yourdomain.com, then the running directory should be set to/www/wwwroot/yourdomain.com/public. In this way, Nginx will first look in thispublicLocate the requested file in the directory.Configure Nginx pseudo-static rulesJust setting the run directory is not enough.Since AnQiCMS is a Go application, its dynamic requests need to be forwarded to the Go program itself.Therefore, you also need to add a reverse proxy rule in the "virtual static" configuration of Nginx.
install.mdorstart.mdProvided in the document) as follows:location @AnqiCMS { proxy_pass http://127.0.0.1:8001; # 这里是AnQiCMS应用监听的端口,默认为8001 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 404 =200 @AnqiCMS; # 如果找不到文件,将请求重写到AnqiCMS处理 location / { try_files $uri $uri/index.html @AnqiCMS; # 核心:优先尝试静态文件,否则转发给AnqiCMS }This rule is:
location /The paragraph is crucial.try_files $uri $uri/index.html @AnqiCMS;Means:- Nginx will first try to find the requested file in
/www/wwwroot/yourdomain.com/public(i.e., the directory you have set as the working directory) for the user request ($uriIf found, it returns directly, which greatly accelerates the loading of static resources. - If not found
$uri, it will try to find$uri/index.html(for example, if the request/about, it will try to find/public/about/index.html). - If all these cannot be found, Nginx will pass the request through
@AnqiCMSThis internal naming rule, forward to the AnQiCMS application running in the backgroundproxy_pass http://127.0.0.1:8001;), handled by Go applications for dynamic routing and content generation.
Through such configuration, Nginx can efficiently serve static content, and seamlessly pass dynamic content requests to AnQiCMS, ensuring the website is both fast and stable.
- Nginx will first try to find the requested file in
Considerations for multi-site deployment
If you plan to deploy multiple AnQiCMS sites on a single server, this principle also applies.Each AnQiCMS instance (if not installed through the built-in multi-site feature of AnQiCMS, but independently installed) requires a separate port and Nginx configuration.publicfolder, and configure the corresponding pseudo-static reverse proxy rules, justproxy_passThe port may vary depending on the listening port of your AnQiCMS instance.
Summary
Correctly configuring Nginx's running directory is a key step in the successful deployment of AnQiCMS. It should point to the root directory of the project.publicfolder, with precise.try_filesandproxy_passThe rule can maximize the use of Nginx's static file service advantages and the efficient processing capabilities of Go applications, providing excellent performance and user experience for your website.When operating the Baota panel, be sure to carefully check these configurations to avoid unnecessary running issues.
Common Questions (FAQ)
Why can't the Nginx running directory be set directly to the project root directory of AnQiCMS?答:If you directly point Nginx's running directory to the project root directory, Nginx will attempt to provide all files under the project root directory directly, which may expose sensitive information such as code files, configuration files, etc. that should not be publicly accessible, posing a security risk. At the same time, since the Go application itself handles dynamic requests, Nginx also needs to be configured with reverse proxy to forward requests to the Go application. Directly pointing to the project root directory will cause Nginx to be unable to effectively distinguish between static files and dynamic requests, thereby making
try_filesThe rules are difficult to effectively take effect, which may instead lead to resource loading errors or security vulnerabilities. Set the running directory to be accurate topublicFolder, ensures that only static resources meticulously prepared by AnQiCMS will be directly provided by Nginx, with the remaining dynamic requests handled by the Go application, achieving a balance between security and performance.Nginx configuration in the
try_filesWhat role does the instruction play here?Answer:try_files $uri $uri/index.html @AnqiCMS;It is a very powerful directive in the Nginx configuration, which defines the priority order of Nginx in processing requests.$uri:Nginx will first try to find a file that matches the request URI completely in the directory you have configured to run (i.e.,)/publicfolder). If the user requests/css/style.css, Nginx will try to find/public/css/style.css.$uri/index.htmlIf the first attempt fails, Nginx will then try to find in the subdirectory corresponding to the URIindex.htmlfile. For example, if the request/about/, Nginx will try to find/public/about/index.html.@AnqiCMS:If both attempts fail, it means that the request is not a static file or the corresponding default file cannot be found, then Nginx will pass the request to the one namedAnqiCMSThe internal naming block processing, this block will forward requests to the Go application running in the background. In short,proxy_passThe instruction will forward requests to the Go application running in the background. In short,try_filesEnsured that Nginx can prioritize and efficiently serve static resources, only forwarding requests to dynamic applications when static resources are not found, thereby optimizing website performance.
If my AnQiCMS application does not listen on port 8001, how should I modify the Nginx configuration?答:AnQiCMS默认监听8001端口,但在某些情况下您可能更改了此端口(例如在English)}]
config.jsonEnglish modification). If your AnQiCMS application actually listens to a different port (for example, 8002), then you need to modify the pseudo-static configuration in Nginx.proxy_passInstructions, point it to the correct port. Specifically, set `proxy_passhttp://127.0.0.1: