Home > Article > Backend Development > Nginx configuration in Vagrant
In series of articles 1 and 2, we introduced the installation of Vagrant and the automatic installation of development environment software.
This article will write about the configuration of Nginx in the virtual machine and how to access Nginx in the real machine.
Open the Vagrantfile file and find the following configuration:
<code>config<span>.vm</span><span>.network</span><span>"forwarded_port"</span>, guest: <span>80</span>, host: <span>8080</span></code>
This configuration means to map port 80 of the virtual machine to port 8080 of the real machine.
Use the vagrant ssh
command to enter the virtual machine
Back up the default nginx configuration file
<code>sudo <span>cp</span> /etc/nginx/nginx<span>.conf</span> /etc/nginx/nginx<span>.conf</span><span>.back</span></code>
Modify the configuration
Open /etc/nginx/nginx.conf
and change the content inside as follows:
<code>events { worker_connections <span>1024</span><span>;</span> } http { server { listen <span>80</span><span>;</span> server_name test<span>.com</span> www<span>.test</span><span>.com</span><span>;</span> charset utf-<span>8</span><span>;</span> location / { root /projects/<span>;</span> index index<span>.html</span> index<span>.htm</span><span>;</span> } <span>#redirect server error pages to the static page /50x.html</span> error_page <span>500</span><span>502</span><span>503</span><span>504</span> /<span>50</span><span>x</span><span>.html</span><span>;</span> location = /<span>50</span><span>x</span><span>.html</span> { root /projects/<span>;</span> } } }</code>
Add an HTML page
In the virtual machine: cd /projects
Create a new index.html or index.htm file in this directory with the following content:
<code><span>html</span>><span>head</span>><span>title</span>>R_Lanffy<span><span>title</span>></span><span><span>head</span>></span><span>body</span>> Hello World <span><span>body</span>></span><span><span>html</span>></span></code>
Access test
Enter the address in the real machine browser: test. com:8080
or www.test.com:8080
to access the nginx related configuration in the virtual machine.
If you want to achieve the purpose of accessing test.com, you need to change the 8080 in the Vagrantfile file to 80
Note: If access is not possible, it is most likely that the 8080 port is before starting the virtual machine. occupied. The solution is to change the port to an unoccupied port.
Check whether the port is being monitored: netstat -an | grep 8080
The above introduces the Nginx configuration in Vagrant, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.