Nginx Simple configuration (Windows environment)

Source: Internet
Author: User
Tags php script tomcat server nginx reverse proxy

I. Installation of Nginx

Unzip the downloaded Nginx compressed package into a directory, such as: D:\program\myplatform\Nginx\nginx-1.10.0, after the unpacked directory structure:

two. Start Configuration

configuration target: Can run the PHP script correctly

In most cases, we need to modify only one of the configuration files, that is nginx.conf, the file is located in the Conf directory. The specific configuration items are:

1. Server_tokens off;

For security reasons, it is best to hide the Nginx version number information

2. Listen 8088;

8088 for listening port, can fill in other port number as needed

3. server_name localhost;

LocalHost is the server access name, which is the URL address that we entered in the browser.

4. CharSet Utf-8;

Character Set encoding

5. Working directory

Configure the following

Location/{
            root  html;
            Index  index.html index.htm;
        }

Modified to:

Location/{
            root   d:/javapro;
            Index  index.php index.html index.htm;
        }

Root defines the workspace, which is the directory where our PHP project resides.

Join index.php is to allow Nginx to identify PHP scripts, otherwise, when accessing PHP files, there will be a direct download.

6. Consolidate PHP

Remove all comments from the location ~ \.php configuration section, and the final configuration is as follows:

Location ~ \.php$ {
            root           d:/javapro;
            Fastcgi_pass   127.0.0.1:9001;
            Fastcgi_index  index.php;
			Fastcgi_param  script_filename  $document _root$fastcgi_script_name;
            Include        fastcgi_params;
        }

Note that the $document_root variable, which corresponds to the root parameter value, if we do not define the root parameter or comment out the root, when accessing PHP, there will be no input file specified on the page. Tips.

7. Start php-cgi

Open the cmd Command window, switch to the installation directory of PHP, the implementation of Php-cgi-b 127.0.0.1:9000, you can start the php-cgi, the start of the completion of the CMD window should not be closed, or php-cgi will be turned off.

Special reminder: Only in the case of open php-cgi, nginx to normal access to PHP.

8. Restart Nginx

Open the cmd Command window, switch to Nginx directory, execute nginx-s reload to restart Nginx. Other related Nginx related orders are as follows:

Start: Start Nginx

Stop: Nginx-s stop

Exit: Nginx-s quit

three. The problems encountered

1. No Log handling enabled-turning on stderr logging

If you report this error during startup php-cgi, please comment out the extension=php_snmp.dll in the php.ini configuration file.

2. Port occupancy

Cannot bind/listen socket-[2] No such file or directory.

Couldn ' t create FastCGI listen socket on port 127.0.0.1:9000

In the process of starting php-cgi, if the report is similar to this error, it means that 9000 ports are occupied, at this time, we can change the port to try.

3. Reverse proxy 404 Not Found

When accessing PHP as a reverse proxy, a 404 Not Found error occurs, and in this case, we need to modify the Error_page configuration entry for Nginx, as follows:

Comment out the original default Error_page

#error_page  404              /404.html;

Then, redefine the error_page as follows:

Error_page 404 @proxy;

The corresponding contents of the @proxy are:

Location @proxy {
        proxy_set_header Host $http _host;
        Proxy_set_header x-real-ip $remote _addr;
        Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

        Proxy_pass http://127.0.0.1;
    }

After saving, restart Nginx to resolve the 404 Not Found error.

Friendship Reminder: In the way of reverse proxy access to PHP, you do not need to configure the php-cgi module.

four. Extended

How to let Nginx support JSP access.

Nginx itself is not supporting dynamic scripts such as JSP, but we can nginx reverse proxy way to support JSP dynamic page access, that is, JSP and other dynamic page requests, forwarding to the back-end of the Tomcat server for processing, the specific configuration as follows:

Under HTTP, create a new server virtual machine with the following contents:

server {
        listen       8089;
        server_name  localhost;
		
        CharSet Utf-8;
	
        Location ~ \. (Jsp|action|do) $ {
            proxy_pass   http://127.0.0.1:8080;
        }
		
        Location ~ \. (html|js|css|png|gif) $ {  
            root d:/program/myplatform/tomcat/webapps/root;  
        }
        
    }

After restarting the Nginx, you can access the Tomcat welcome page through the http://localhost:8089/index.jsp. Where http://127.0.0.1:8080 is the access address of the Tomcat server, Proxy_pass forwards requests such as JSP to the 8080 port of the Tomcat server, noting the static resource portion of the configuration:

Location ~ \. (html|js|css|png|gif) $ {  
            root d:/program/myplatform/tomcat/webapps/root;  
        }

If you remove the above configuration, through the http://localhost:8089/index.jsp will not be able to access the static resources in the JSP file, resulting in the page layout confusion and so on.

In the same way, you can also use this approach to consolidate the access to PHP pages, but to install the Apache server in advance.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.