IIS is used as a web server in win7. However, due to project requirements, automatic redirection is required when the server encounters a 404 error (not a client jump, however, when the server receives a request from the client to read the file from a directory and returns the result, if the file in the directory or directory does not exist, it will automatically go to another server for retrieval), it is difficult to achieve this through IIS, therefore, we decided to build the nginx development environment and achieve this through configuration.
First, google nginx and php will download the latest version from their official website. At that time, my PHP version was 5.4.3 and nginx was 1.5.2, I put them all in the folder named webserver next to drive D, and then began to configure.
Nginxis easy to start. After accessing the website, you can double-click nginx.exe. If there is no port conflict or other issues, you can directly enter localhost in the browser to access the webpage with the word "welcome to nginx". This is the default welcome page, put it in the built-in default webpage file directory html. Is it that simple? Of course not.
At this time, if you create a php file in the html directory, write the most classic php environment test, and then enter the php file path in the browser for access, you will find that nginx does not know how to parse the php code, because nginx and PHP are not working together yet. In principle, when encountering a php file, nginx should hand it over to fast-cgi of php for processing, and then return the processed result to the client (browser.
How can I tell nginx who will handle PHP files? Open the nginx1.5.2 folder, find the conf directory, and edit the nginx. conf file under the directory. My editing results are as follows. For details about the configuration process, see the notes:
Server {
Listen 80;
Server_name localhost;
Charset UTF-8;
Location /{
Root E:/work/jentian/client; // html file directory. Because the client code of our project is strictly separated from the server service, the root here is different from the root of php.
Index index.html index.htm;
}
Location ~ . Php {
Root E:/work/jentian/server; // PHP file directory
Fastcgi_param SCRIPT_FILENAME E:/work/jentian/server $ fastcgi_script_name; // note that the path before $ fastcgi_script_name must be consistent with that of root.
Fastcgi_index index. php;
Fastcgi_pass 127.0.0.1: 9000;
Include fastcgi_params;
}
}
Nginx has so many configurations. It's easy, but when you refresh the php page, you will find that nginx still has no way to parse the php file, the reason is that php's fast-cgi has not been started. If you open a common Command Prompt window and enter a command to start fast-cgi, the window cannot be closed after startup, otherwise, the fast-cgi process will be terminated and the PHP file cannot be parsed. Later, I checked that windows had a method to run the invisible console, if you start fast-cgi using this method, you will not be afraid of the problem that fast-cgi will end after the console is closed. Finally, a batch file is written to start fast-cgi and nginx, which can start the nginx + php environment with one click. The content of the batch processing file is as follows:
@ Echo off
Sets PHP_FCGI_MAX_REQUESTS = 1000
Echo Starting PHP FastCGI...
In rem, The php-cgi.exe and php. ini paths are replaced by spaces.
RunHiddenConsole D:/webServer/php-5.4.3/php-cgi.exe-B 127.0.0.1: 9000-c D:/webServer/php-5.4.3/php. ini // here
Echo Starting nginx...
Replace rem with your nginx directory
D:/webServer/nginx-1.5.2/nginx.exe-p D:/webServer/nginx-1.5.2/
Cd D:/webServer/nginx-1.5.2/
Pause
Save the batch processing file and double-click it to start your nginx + php environment. Then, refresh your phpinfo page, and everything is normal.