Start two nginx instances on linux

    Starting two nginx instances on one machine is mainly due to port conflicts. In addition, if one is installed through compilation and the other is copied, then the root path of the static file needs to be modified.

    Here, my first nginx is compiled and installed through source code, and the installation directory is the default /usr/local/nginx.

    The second nginx copies /usr/local/nginx to the /root/ directory, so the second nginx directory is /root/nginx.

    Here we mainly need to modify the second nginx port, assuming we set it to 9000.

    Here, in order to distinguish the first nginx, we modify the index.html static page under html, and the content is slightly modified:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx2!</h1>
</body>
</html>

    Start nginx. To start here, we need to specify the configuration file through -c. There is a problem with the nginx specified configuration file. The relative path cannot be set and the absolute path needs to be set . 

    We view the configuration file through the test command:

    The first time we used the relative path conf/nginx.conf, this specification did not take effect, but the default first nginx configuration file was used. At this time, if you continue to start, it will report an error saying that port 80 is already occupied.

    nginx starts the specified configuration file, and the path must use an absolute path.

    When we happily start the configuration file by specifying the absolute path, and then access it, we find that we are accessing the first nginx home page. At this time, you need to modify the static file root path root. The configuration is modified like this:

location / {
    root   /root/nginx/html/;
    index  index.html index.htm;
}

     After that, start again, visit the home page, 403:

     Then modify the nginx.conf configuration, open the configuration comment in the first line, and modify user nobody to user root; 

    The last access was successful:

    In this way, the twists and turns of dual nginx startup are completed. 

    Finally, let's look at some details by looking at the process:

    The first nginx worker process uses nobody to start, and the other one uses root by default, so the above 403 problem requires modifying the user root; the configuration file is specified here through the absolute path, and it is also listed when displayed. 

    To summarize the caveats:

     1) nginx needs to use an absolute path to start the specified configuration file.

     2) Static resource configuration root path, the second configuration needs to use an absolute path. Using a relative path, or the default value, the first nginx static file is accessed.

      3) 403 appears when accessing nginx. You need to modify the user attribute value of nginx.conf configuration to root.

      4) After modifying the configuration, to make the configuration take effect, you can directly use the command:

sbin/nginx -c /root/nginx/conf/nginx.conf -s reload

おすすめ

転載: blog.csdn.net/feinifi/article/details/131264540