Installation and configuration examples of Windows Nginx

Source: Internet
Author: User

This article mainly share with you the installation and configuration examples of Windows Nginx, hope to help everyone.

First, Nginx Introduction

What is 1.Nginx?

Nginx is a lightweight Web server, but also a reverse proxy server

What can 2.Nginx do?

① can directly support rails and PHP programs
② can be used as an HTTP reverse proxy server
③ as a Load balancer server
④ as the Mail proxy server
⑤ helps achieve front-end static and dynamic separation

3.Nginx Features

    • High stability

    • Performance

    • Low resource footprint

    • Feature Rich

    • Modular construction

    • Support for hot deployment

Second, Nginx installation

1. Download: Http://nginx.org/download/nginx-1.10.2.zip

2. Unzip

3. Run Nginx.exe: run by double-clicking the icon or CMD command line

Three, Nginx common commands

1. Test the configuration file

Nginx.exe-t under the installation path

2. Start the command

Nginx.exe under the installation path

3. Stop command

Nginx.exe-s stop under the installation path,
Or: Nginx.exe-s quit

4. Restart command

Nginx.exe-s reload under the installation path

5. View process Commands

Ps-ef |grep Nginx

6. Smooth Restart

Kill-hup "Nginx main process number (that is, view process command to find the PID)"

7. Increase firewall access rights

①sudo Vim/etc/sysconfig/iptables
②-a input-p tcp-m state–state NEW
-M Tcp–dport 80-j ACCEPT
③ Save exit
④ Restart firewall sudo service iptables restart

Four, nginx virtual domain name configuration and test verification

Configuration steps:

1. Edit sudo vim/usr/local/nginx/conf/nginx.conf
① added include vhost/*.conf;
② Save exit

2. Create a new Vhost folder in the/usr/local/nginx/conf/directory:
namely:/user/local/nginx/conf/vhost

3. Create a domain name forwarding profile

image.hcxjingdong.com.conf: Reverse proxy to the directory: server {listen 80;    AutoIndex off;    server_name image.hcxjingdong.com; Access_log c:/access.log combined; Index index.html index.htm index.jsp index.php; #error_page 404/404.    html if ($query _string~ * ". *[\; '    \<\>].* ") {return 404;     } location~/(Mmall_fe|mmall_admin_fe)/dist/view/* {deny all;         } location/{root C:\ftpfile\img;         Add_header Access-control-allow-origin *;    }}tomcat.hcxjingdong.com.conf: Reverse proxy for the steering port: server {Listen 80;    AutoIndex on;    server_name tomcat.hcxjingdong.com; Access_log c:/access.log combined; Index index.html index.htm index.jsp index.php; #error_page 404/404.    html if ($query _string~ * ". *[\; '    \<\>].* ") {return 404;             } location/{Proxy_pass http://127.0.0.1:8080;             Add_header Access-control-allow-origin *; }     }

4. Startup (restart) verification
① Boot:

{Nginx}/sbin/nginx-s Reload

Note: ${nginx} represents the path installed in the system, for example:/usr/local/nginx

5. Access Verification

Use default 80 port access authentication: HTTP://LOCALHOST:80 or http://127.0.0.1:80

6. Point to Port

HTTP forwarding

server{    Listen;    AutoIndex off;    server_name learning.hcxjingdong.com;    Access_log C:/access.log combined;    Index index.html index.htm index.jsp index.php;    #error_page 404/404.html;    if ($query _string ~* ". *[\; ' \<\>].* ") {        return 404;    }    Location/{        Proxy_pass http://127.0.0.1:81/learning;        Add_header Access-control-allow-origin *;    }}

Listen 80: monitor 80 ports;
AutoIndex off: Whether to create the index directory of the homepage;
When Nginx receives image.hcxjingdong.com (level two domain name) request, it forwards to: Http://127.0.0.1:81/learning directory

7. Point to Directory

Wire server, front-end deployment servers for the front end are all through a reverse proxy to the directory

server{    Listen;    AutoIndex off;    server_name img.hcxjingdong.com;    Access_log C:/access.log combined;    Index index.html index.htm index.jsp index.php;    #root/product/front/;    #error_page 404/404.html;    if ($query _string ~* ". *[\; ' \<\>].* ") {        return 404;    }    Location ~/(Hcxjingdong_fe|hcxmall_admin_fe)/dist/view/* {        deny all;    }    Location/{        root \product\ftpfile\img;        Add_header Access-control-allow-origin *;    }}

ROOT/PRODUCT/FTPFILE/IMG:
Root directly points to the IMG folder under Ftpfile under the hard Disk System directory product folder;
That is, when accessing img.hcxjingdong.com, it points directly to the folder

8. Test validation

Five, Nginx precautions

You can configure the forwarding of the domain name, but be sure to configure the host and have the host in effect before you can restart the browser after Setup is complete

Under Windows configuration:
① into C:\Windows\System32\drivers\etc
② Open the Hosts file with Notepad
③ add the corresponding domain name and IP
④ Save exit

For example:
10.211.55.6 image.hcx.com
10.211.55.6 s.hcx.com

Six, configure Nginx under Windows

Configure hosts:
C:\Windows\System32\drivers\etc

Accessing www.hcxjingdong.com in a browser

Includes native access http://localhost:

Configuring the Forwarding of directories

1. Go to nginx.conf (nginx main configuration):
Add: Include vhost/*.conf;

2. Follow the path to create this folder:
Create a Vhost folder under the Conf folder

3. Create a file in the Vhost folder: image.hcxjingdong.com.conf

File contents:

server{    Listen;    AutoIndex off;    server_name image.hcxjingdong.com;    Access_log C:/access.log combined;    Index index.html index.htm index.jsp index.php;    #error_page 404/404.html;    if ($query _string ~* ". *[\; ' \<\>].* ") {        return 404;    }    Location ~/(Hcxmall_fe|hcxmall_admin_fe)/dist/view/* {        deny all;    }    Location/{        root C:\ftpfile\img;        Add_header Access-control-allow-origin *;    }}

Store images in C:\ftpfile\img directory for access

4. Modify the host of this machine, let the native Nginx cooperate to the image.hcxjingdong.com domain name

Go to the C:\WINDOWS\SYSTEM32\DRIVERS\ETC directory to modify the Hosts file:

5. Restart Nginx:

Go to the Nginx directory to execute the command:
①NGINX.EXE-T: Verify that the configuration file is correct
②nginx.exe-s Reload: Restart Nginx

6. Access the domain name (image.hcxjingdong.com) to verify that the picture is valid:

Test if host is in effect: image.hcxjingdong.com
Test whether the picture is in effect: http://image.hcxjingdong.com/hcx.jpg

Configuring the forwarding of IP ports

1. Created under conf vhost: tomcat.hcxjingdong.com.conf

IP port forwarding using a Tomcat domain name, forwarded to the Tomcat service

Tomcat.hcxjingdong.com.conf:

server{    Listen;    AutoIndex off;    server_name tomcat.hcxjingdong.com;    Access_log C:/access.log combined;    Index index.html index.htm index.jsp index.php;    #error_page 404/404.html;    if ($query _string ~* ". *[\; ' \<\>].* ") {        return 404;    }    Location/{        Proxy_pass http://127.0.0.1:8080;        Add_header Access-control-allow-origin *;    }}

2. Configure the hosts:

3. Start Tomcat

4. Restart Nginx:nginx.exe-s Reload

5. Access http://tomcat.hcxjingdong.com
successfully displays the Tomcat startup page, stating that HTTP forwarding is also successful.

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.