Reprint: Original link: https://www.cnblogs.com/taiyonghai/p/6728707.html
nginx Detailed installation Deployment Tutorial
Introduction of Nginx
Nginx is a Web server can also be used to do load balancing and reverse proxy use, the current use of the most is load balance, specific introduction I will not introduce Baidu a lot, the following directly into the installation steps
Second, Nginx installation
1, download Nginx and related components
Linux system is CentOS 6.5 64 bits, I switch directly to the root user installation
Go to user directory download program
Download related components
[Root@localhost src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz
omit installation content ...
[Root@localhost src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz
omit installation content ...
[Root@localhost src]# wget http://zlib.net/zlib-1.2.11.tar.gz
omit installation content ...
[Root@localhost src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
omit installation content ...
Install the C + + compilation environment, if you have installed to skip
[Root@localhost src]# yum install gcc-c++
omit installation content ...
There will be a confirmation prompt to enter Y return is this
OK [y/n]:y
omitted installation content ...
2, installation of Nginx and related components
OpenSSL installation
[Root@localhost src]# tar zxvf openssl-fips-2.0.10.tar.gz
Omit installation content ...
[root@localhost src]# cd openssl-fips-2.0.10
[root@localhost openssl-fips-2.0.10]#./config && Make && Amp Make install
Omit installation content ...
Pcre Installation
[Root@localhost src]# tar zxvf pcre-8.40.tar.gz
Omit installation content ...
[root@localhost src]# cd pcre-8.40
[root@localhost pcre-8.40]#./configure && make && make install
Omit installation content ...
Zlib installation
[Root@localhost src]# tar zxvf zlib-1.2.11.tar.gz
Omit installation content ...
[root@localhost src]# cd zlib-1.2.11
[root@localhost zlib-1.2.11]#./configure && make && make Install
Omit installation content ...
Nginx Installation
[Root@localhost src]# tar zxvf nginx-1.10.2.tar.gz
Omit installation content ...
[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]#./configure && make && make Install
Omit installation content ...
3, start Nginx
Let's find out where Nginx is installed.
Enter the Nginx directory and start
Error has been made, while loading shared libraries:libpcre.so.1:cannot open Shared object file:no such file or directory is resolved in the following manner
1. Use the Whereis libpcre.so.1 command to find where the libpcre.so.1 is
. 2. Make a soft connection with ln-s/usr/local/lib/libpcre.so.1/lib64 command
3. With sbin/ Nginx start Nginx
4 with Ps-aux | grep nginx View status
[Root@localhost nginx]# Whereis libpcre.so.1
[Root@localhost nginx]# ln-s/usr/local/lib/libpcre.so.1/lib64
[Root@localhost nginx]# Sbin/nginx
[Root@localhost nginx]# Ps-aux | grep nginx
Into the Linux system's graphical interface, open the browser input localhost will see the following figure, indicating Nginx started successfully
Basic operation of Nginx
Start
[root@localhost ~]#/usr/local/nginx/sbin/nginx
stop/reboot
[root@localhost ~]#/usr/local/nginx/sbin/ Nginx-s Stop (quit, reload)
command Help
[root@localhost ~]#/usr/local/nginx/sbin/nginx-h
authentication Profile
[ Root@localhost ~]#/usr/local/nginx/sbin/nginx-t
configuration file
[root@localhost ~]# vim/usr/local/nginx/conf/ Nginx.conf
4, simple configuration Nginx
Open the Nginx configuration file in the Conf folder under the Nginx directory
A brief introduction to Vim's grammar
The default vim can not be entered after opening, you need to press to operate, as follows:
Open edit: Press "I" or "Insert" key to
exit the editor: "ESC" key to
exit Vim: ": Q"
save Vim: ": w"
Save exit Vim: ": Wq"
do not save quit vim: ": q!"
"#" represents the annotation, the most important is the server{} block this section represents each Web site, detailed configuration introduction can check out my another piece of configuration article, here we set up three sites temporarily
Use different ports 80, 81, 82 to save the exit and restart Nginx separately
5, open the external network access
In the Linux system, the default firewall iptables all the ports, only enable the default remote connection 22 port all other closed, we set the above 80 and so on is also closed, so we need to first open the application port
Method A direct shutdown of the firewall, this performance is good, but less secure, if there is a front firewall can take this way
Shut down firewall
[root@localhost ~]# service iptables stop
shutdown boot from boot firewall
[root@localhost ~]#
chkconfig iptables off [Root@localhost ~]# chkconfig--list|grep IPT
The following are other operational commands for the firewall
Method Two will be open port to join the firewall white list, this way is safer but the performance is also relatively poor
Edit firewall Whitelist
[root@localhost ~]# vim/etc/sysconfig/iptables
Add the following line of code
-A input-p tcp-m State--State new-m TCP --dport 80-j ACCEPT
Save exit, reboot firewall
[root@localhost ~]# service iptables restart
Linux configuration completed, using another computer instead of installing Nginx computer, I am using the Windows system, configure the host in the "C:\Windows\System32\drivers\etc" In the hosts below configure the domain name Redirection (explanation: Because the domain name configured on the Ngnix nginx.test.com, is not a real backup of the domain name)
10.11.13.22 nginx.test.com nginx.test1.com nginx.test2.com
Then cmd ping again the domain name is correct point to this IP
After pointing correctly, Telnet to port 80 to see if you can communicate with the port (if the Telnet prompt does not install the client without this command, you can then do so after you enable or disable the Windows features installation)
Get the following interface and represent successful communication
Open the browser in this Windows system, enter nginx.test.com will get the following results, the success of the extranet access
If the firewall you are still enabled, just set the Enable port, then we access 81 that port will find inaccessible, because I did not join the white list
To this Nginx server prototype deployment is complete.
6. Nginx Load Balance Configuration
Nginx set reverse proxy and load balancing in a configuration file can be modified with the implementation of
First we open the configuration file
[Root@localhost nginx]# Vim conf/nginx.conf
Each server is a virtual host, we have a Web server to use as a
Listen 80, on behalf of listening to 80-port
server_name xxx.com, domain name
location/{} for extranet access, representing a filter,/matching all requests, we can also define different filters according to our own situation. For example, the static file js, CSS, image to create exclusive filter
root HTML; represents the site root
index index.html; represents the default home page
This configuration is complete, we enter the domain name can be accessed to the site.
Load-balancing functions are often allocated to multiple servers on the back end after receiving a request, so we need upstream{} blocks to work with
Upstream Xxx{};upstream module is named a back-end server group, the group name must be the backend server site domain name, the internal can write multiple servers IP and port, you can set the jump rules and weights and so on
Ip_hash; Represents the use of an IP address to assign a jump back-end server, with the same IP request accessing the same back-end server
; representing the backend server address
Server{};server module is still part of receiving external requests
Server_ Name; represents the extranet access domain name
location/{}; It also represents filters for different operations proxy_pass for different requests
, and for backend server group names, which must be the back-end server site domain name
Server_ The group name of name and upstream{} can be inconsistent, server_name is an extranet access to the domain name that receives the request, upstream{} 's group name is the domain name that the site accesses when you jump back-end server
Configure Windows host to aaa.test.com the domain name we are accessing to Linux
Because of the limited hardware, I am using IIS in Windows as a nginx back-end server, so configure the IIS site domain name
Open cmd and ping again aaa.test.com really point to the Linux system, and then open the browser input aaa.test.com will show BBB this site on behalf of the success of the load.
Nginx load function on the configuration completed, this is simply set up a bit, the production environment there are a lot of detailed adjustment, followed by gradually increase, my level is limited, if there is a wrong place also hope guidance thank you.