As a senior CMS website operation personnel, I know that stable and efficient deployment is the foundation of successful website operation.Under Linux, Nginx, as a high-performance reverse proxy server, is an ideal partner for AnQiCMS, which can effectively improve the access speed and security of the website.The following will introduce how to configure Nginx under the Linux command line environment to provide a stable and reliable reverse proxy service for AnQiCMS.

Configure AnQiCMS Nginx Proxy in Linux Command Line Environment

English CMS (EnglishCMS) is an enterprise-level content management system developed based on Go language, known for its easy deployment, fast execution speed, and SEO-friendly features, and is the preferred choice for many small and medium-sized enterprises and content operation teams.On Linux servers, running AnQiCMS usually requires Nginx for reverse proxying, so that users can access the website through standard 80 or 443 ports and enjoy the advantages of Nginx such as static file acceleration and SSL encryption.

Preparation: Ensure that AnQiCMS is running on the server

Before configuring Nginx, you need to ensure that the AnQiCMS program has been downloaded and successfully run on your Linux server.通常,AnQiCMS will listen on a specific port, such as the default port 8001.

First, download the installation package for Linux from the AnQiCMS official website. Extract it to the directory of your choice, for example,/www/wwwroot/yourdomain.com/In this directory, you will find the executable file of AnQiCMS (usually namedanqicms) as well as astart.shscript.

To ensure that AnQiCMS can also run automatically after the server restarts, it is recommended that you add it to the system's scheduled tasks (Crontab). By runningcrontab -eCommand to open the Crontab editor, then add the following line:

*/1 * * * * /www/wwwroot/yourdomain.com/start.sh

Please make sure to put/www/wwwroot/yourdomain.com/Replace it with the actual installation path of AnQiCMS.This command checks every minute if AnQiCMS is running, and if it is not, it starts it to ensure the continuous availability of the service../start.shCommand to ensure that the AnQiCMS service is already running.

Configure Nginx reverse proxy.

Next, we will configure Nginx to receive user requests and forward them to the port that AnQiCMS is listening on. The Nginx configuration file is usually located/etc/nginx/sites-available/or/etc/nginx/conf.d/Directory. You need to create a new configuration file for your AnQiCMS site, for exampleyourdomain.com.conf.

Use your favorite text editor (such as)viornano) to create and edit this file:

sudo nano /etc/nginx/sites-available/yourdomain.com.conf

Then, paste the following Nginx configuration code into the file. Please modify it according to your actual situation.server_nameandrootCommand.

server
{
    listen 80; # 监听标准的HTTP端口
    # listen 443 ssl http2; # 如果您配置了SSL证书,请取消注释并配置SSL相关指令
    server_name yourdomain.com www.yourdomain.com; # 您的网站域名,可以添加多个

    # 网站根目录,指向AnQiCMS安装目录下的public文件夹
    root /www/wwwroot/yourdomain.com/public;

    # 配置Nginx反向代理,将请求转发给AnQiCMS
    location @AnqiCMS {
        proxy_pass http://127.0.0.1:8001; # AnQiCMS监听的端口,默认为8001
        proxy_set_header Host $host; # 转发原始请求的Host头
        proxy_set_header X-Real-IP $remote_addr; # 转发客户端的真实IP地址
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 转发客户端代理链的IP地址
        # proxy_set_header X-Forwarded-Proto $scheme; # 如果使用SSL,请取消注释
    }

    # 处理404错误,将所有未找到的请求路由到AnQiCMS处理
    error_page 404 =200 @AnqiCMS;

    # 尝试查找文件,如果找不到则交给AnQiCMS处理
    location / {
       try_files $uri $uri/index.html @AnqiCMS;
    }

    # 设置访问日志,您可以指定日志文件的路径
    access_log /var/log/nginx/yourdomain.com_access.log;
    error_log /var/log/nginx/yourdomain.com_error.log;
}

Configuration details:

  • listen 80;: Nginx监听HTTP请求的标准端口。如果您希望启用HTTPS,还需要配置listen 443 ssl http2;并添加SSL证书路径等相关配置。
  • server_name yourdomain.com www.yourdomain.com;: Specify your website domain. When users access through these domains, Nginx will apply this configuration.
  • root /www/wwwroot/yourdomain.com/public;This is a very critical step. It will point Nginx's website root directory to the AnQiCMS installation directory.publicFolder.AnQiCMS's static resources (such as CSS, JS, images, etc.) are usually stored in this directory, and Nginx can directly serve these static files without going through the AnQiCMS program.
  • location @AnqiCMS { ... }: This is a named location block used to define how requests are proxied to AnQiCMS.
    • proxy_pass http://127.0.0.1:8001;: This instruction is core, it tells Nginx to forward all matched requests to the local@AnqiCMS.8001Port, that is the address where AnQiCMS service is listening. If you have modified the running port of AnQiCMS, please make the corresponding modification here.
    • proxy_set_header ...: These instructions are used to pass the original client request header information (such as Host, real IP, etc.) to AnQiCMS. This is crucial for AnQiCMS to obtain the correct client information and log recording.
  • error_page 404 =200 @AnqiCMS;: When Nginx attempts to locate a file but returns a 404 error, it will internally redirect the request to@AnqiCMSProcessing, which means AnQiCMS will be responsible for handling all missing URLs, achieving a friendly URL structure and pseudo-static.
  • location / { try_files $uri $uri/index.html @AnqiCMS; }: This is the default request handling block. It first tries to find a file matching the request URI inrootthe directory.index.htmlIf found, return directly. If the static file does not exist, forward the request.@AnqiCMSName the location block processing, so that the content is dynamically generated by AnQiCMS.

Save and close the configuration file, then you need to create a symbolic link to activate the configuration file to Nginx'ssites-enabledDirectory:

sudo ln -s /etc/nginx/sites-available/yourdomain.com.conf /etc/nginx/sites-enabled/

Check the Nginx configuration and restart

Before restarting the Nginx service, make sure to check the syntax of the configuration file to avoid service startup failure:

sudo nginx -t

If the output showssyntax is okandtest is successfulIt indicates that the configuration is correct. Now, you can reload the Nginx service to apply the new configuration:

sudo systemctl reload nginx
# 或者
sudo service nginx reload

Verify deployment

Complete the Nginx configuration and restart the service, then you can enter your domain name (such ashttp://yourdomain.com)来访问AnQiCMS网站。如果一切配置正确,您将看到AnQiCMS的初始化安装界面或已部署的网站内容。

Through the above steps, you have successfully configured Nginx reverse proxy for AnQiCMS under the Linux command-line environment, providing a high-performance, highly available access entry for your website.


Common Questions and Answers (FAQ)

1. Why can't the CSS, JS, or images of the website be loaded normally after configuring Nginx reverse proxy?

This is usually due to Nginx'srootInstruction configuration is incorrect, or the static file path of AnQiCMS does not match the expected path of Nginx. Please ensure that the Nginx configuration in theroot指令精确指向AnQiCMS安装目录下的publicfolder, for exampleroot /www/wwwroot/yourdomain.com/public;.Nginx will directly serve static resources from this directory.Additionally, check that the "Global Settings" in AnQiCMS backend has the "Website Address" and "Template Static Files Address" correctly set, ensuring they match the domain and path after Nginx proxying.

2. 我想在同一台服务器上运行多个AnQiCMS站点,应该如何配置Nginx?

AnQiCMS supports multi-site management, you only need one AnQiCMS core program to run multiple sites. At the Nginx level, you need to create an independent site for each AnQiCMS site.server块。Eachserverblockserver_namecommand should correspond to different domains, and each siterootcommand still points to the same AnQiCMS core programpublicdirectory. AnQiCMS will respond to the requestedHostHead (Nginx throughproxy_set_header Host $host;Passed) to identify which site is being accessed, and present the corresponding content.In AnQiCMS backend multi-site management, you need to add a site configuration for each new domain and ensure that the database and root directory settings are correct.

3. If the default port 8001 of AnQiCMS is occupied, how can I solve it?

When AnQiCMS fails to start and prompts that the port is occupied, you can choose to change the listening port of AnQiCMS. This is usually done by editing the AnQiCMS installation directory underconfig.jsonThe file to implement. FindportField and change it to another unused port, for example8002。After the change, you need to restart the AnQiCMS service and update the Nginx configuration file accordinglyproxy_passthe port number of the command, for example,proxy_pass http://127.0.0.1:8001;Change toproxy_pass http://127.0.0.1:8002;. After updating the Nginx configuration, don't forget to check the syntax and reload the Nginx service.