Nginx Learning Notes

Source: Internet
Author: User

Author: fbysss
Msn:jameslastchina@hotmail.com
Blog:blog.csdn.net/fbysss
Statement: This article is prepared by FBYSSS, reproduced please indicate the source
Keywords: Nginx

Nginx, it's cool.

First compile Pcre: Enter the Extract directory

./configure--prefix=/opt/pcre

Make

Makeinstall

It should be noted that the default is problematic. The following actions are required

Mkdir/usr/local/include/pcre

Mkdir/usr/local/include/pcre/.libs

Cp/opt/pcre/lib/libpcre.a/usr/local/include/pcre/libpcre.a

Cp/opt/pcre/lib/libpcre.a/usr/local/include/pcre/libpcre.la

Cp/opt/pcre/include/pcre.h/usr/local/include/pcre/pcre.h

Cp/usr/local/include/pcre/*.*/usr/local/include/pcre/.libs

./configure--prefix=/opt/nginx-with-pcre=/usr/local/include/pcre

Note that the path is not followed by a slash.

In make, delete the words inside the Objs/makefile

Find the./configure--disable-shared keyword, delete the line, and then make

Configuration:

Nginx configuration, simply too human, in addition to the configuration of the cluster, the other I almost did not look at the help document, is entirely a matter of thought, a little bug is not, simply too smooth.

Virtual Host: Can be equipped with multiple,

Listen xxx.xxx.xxx [Port]

servername [whether need to configure in the/etc/hosts is not necessarily ( do not need ), here can also be multiple, separated by a space, such as I can do picture server, but also do load balancing server, but the port number is not the same, then configure two virtual hosts, Port listening is not the same. ]

For example, the image server is on the local 192.168.0.2, then there are two tomcat on 192.168.0.3 and 192.168.0.4.

Configuring in the HTTP segment

Upstream tomcats{

192.168.0.3:8080

192.168.0.3:8080

}

One problem to note: If Nginx's listening port is not 80, sometimes there is a problem, forwarding is not with the top number, this may be a bug. The configuration is 80 and there is no problem.

Then in location/, configure

Proxy_pass Tomcats, the reverse proxy, implements distribution

Virtual Directory configuration:

location/hello{

Alias/usr/local/sss/hello;

}

the other thing to be aware of is Realip configuration

 

Proxy_set_header Host $host;

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

nginx+ (1...N) Tomcat cluster deployment, back-end Tomcat needs to obtain the user's IP, when the request.getremoteaddr () is always achieved by 127.0.0.1.

As a result of the user request after NGINX,NGIGX forwarded to Tomcat, Tomcat has always been nginx IP, if you need to obtain real user IP, only to do some configuration on the Ngix.

Location/down setting

Proxy_set_header X-real-ip $remote _addr;

Proxy_set_header Host $host;

Test configuration:./sbin/nginx-t-C conf/nginx.conf reboot nginx after correct.

The Java code is modified as follows

Out.println ("X-real-ip:" + request.getheader ("X-real-ip") + "<br>");

Where X-real-ip is the real IP of the user

Load Balancing Strategy

Upstream tomcats{

Ip_hash;

Server 192.168.0.3:8080;

Server 192.168.0.4:8080;

}

server {

Location/{

Proxy_pass Http://backend;

}

Depending on the client IP load Balancing, select the same source server to the backend for the client in the same Class C address segment to avoid the problem caused by the session.

 

Also, Nginx does not provide a start-and-stop script and needs to be written by itself.

#! /bin/sh

### BEGIN INIT INFO
# Provides:nginx
# Required-start: $all
# Required-stop: $all
# Default-start:2 3 4 5
# default-stop:0 1 6
# short-description:starts the Nginx Web server
# description:starts Nginx using Start-stop-daemon
### End INIT INFO

Path=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
Daemon=/usr/sbin/nginx
Name=nginx
Desc=nginx

Test-x $DAEMON | | Exit 0

# Include Nginx Defaults if available
if [-f/etc/default/nginx]; Then
. /etc/default/nginx
Fi

Set-e

Case "$" in
Start
Echo-n "Starting $DESC:"
Start-stop-daemon--start--quiet--pidfile/var/run/$NAME. pid
--exec $DAEMON--$DAEMON _opts
echo "$NAME."
;;
Stop
Echo-n "Stopping $DESC:"
Start-stop-daemon--stop--quiet--pidfile/var/run/$NAME. pid
--exec $DAEMON
echo "$NAME."
;;
Test
$DAEMON-T-c/etc/nginx/conf/nginx.conf
;;
Restart|force-reload)
Echo-n "Restarting $DESC:"
Start-stop-daemon--stop--quiet--pidfile
/var/run/$NAME. PID--exec $DAEMON
Sleep 1
Start-stop-daemon--start--quiet--pidfile
/var/run/$NAME. PID--exec $DAEMON--$DAEMON _opts
echo "$NAME."
;;
Reload
Echo-n "Reloading $DESC configuration:"
Start-stop-daemon--stop--signal HUP--quiet--pidfile/var/run/$NAME. pid
--exec $DAEMON
echo "$NAME."
;;
Upgrade
Echo-n "Upgrade $DESC:"
KILL-USR2 ' cat/var/run/$NAME. PID '
Sleep 1
Kill-winch ' cat/var/run/$NAME. PID '
Sleep 1
Kill-9 ' cat/var/run/$NAME. Pid.oldbin '
rm/var/run/$NAME. Pid.oldbin
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|test|restart|reload|force-reload|upgrade}" >&2
Exit 1
;;
Esac

Exit 0

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.