{"id":7295,"date":"2018-03-31T08:54:14","date_gmt":"2018-03-31T00:54:14","guid":{"rendered":"http:\/\/rmohan.com\/?p=7295"},"modified":"2018-03-31T08:54:14","modified_gmt":"2018-03-31T00:54:14","slug":"nginx-is-a-high-performance","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7295","title":{"rendered":"nginx is a high performance"},"content":{"rendered":"<p>nginx is a high performance web server software. It is a much more flexible and lightweight program than apache.<\/p>\n<p>yum install epel-release<\/p>\n<p>yum install nginx<\/p>\n<p>ifconfig eth0 | grep inet | awk &#8216;{ print $2 }&#8217;<\/p>\n<p>wget \u2013no-cookies \u2013no-check-certificate \u2013header \u201cCookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie\u201d \u201chttp:\/\/download.oracle.com\/otn-pub\/java\/jdk\/8u60-b27\/jdk-8u60-linux-x64.tar.gz\u201d<br \/>\nwget http:\/\/mirror.nus.edu.sg\/apache\/tomcat\/tomcat-8\/v8.0.30\/bin\/apache-tomcat-8.0.30.tar.gz<br \/>\ntar xzf jdk-8u40-linux-i586.tar.gz<br \/>\nmkdir \/usr\/java\/<\/p>\n<p>cd \/usr\/java\/jdk1.8.0_40\/<br \/>\n[root@cluster1 java]# ln -s \/usr\/java\/jdk1.8.0_40\/bin\/java \/usr\/bin\/java<br \/>\n[root@cluster1 java]# alternatives \u2013install \/usr\/java\/jdk1.8.0_40\/bin\/java java \/usr\/java\/jdk1.8.0_40\/bin\/java 2<\/p>\n<p>alternatives &#8211;install \/usr\/java\/jdk1.8.0_40\/bin\/java java \/usr\/java\/jdk1.8.0_40\/bin\/java 2<br \/>\nalternatives &#8211;config java<\/p>\n<p>vi \/etc\/profile.d\/java.sh<br \/>\nexport JAVA_HOME=\/usr\/java\/jdk1.8.0_25<br \/>\nPATH=$JAVA_HOME\/bin:$PATH<br \/>\nexport PATH=$PATH:$JAVA_HOME<br \/>\nexport JRE_HOME=\/usr\/java\/jdk1.8.0_25\/jre<br \/>\nexport PATH=$PATH:\/usr\/java\/jdk1.8.0_25\/bin:\/usr\/java\/jdk1.8.0_25\/jre\/bin<\/p>\n<p>Three, Tomcat load balancing configuration<\/p>\n<p>When Nginx start loading default configuration file \/etc\/nginx\/nginx.conf, while nginx.conf in references \/etc\/nginx\/conf.d catalog all .conf files.<\/p>\n<p>Therefore, some of their own custom configuration can be written to a separate .conf files, as long as the files are placed \/etc\/nginx\/conf.d this directory can be, and easy maintenance.<\/p>\n<p>Create tomcats.conf: vi \/etc\/nginx\/conf.d\/tomcats.conf, which reads as follows:<\/p>\n<p>\/usr\/tomcat\/apache-tomcat-8.0.30\/bin\/startup.sh<\/p>\n<p>vi \/etc\/nginx\/conf.d\/tomcats.conf<\/p>\n<p>upstream tomcats {<br \/>\nip_hash;<br \/>\nserver 192.168.1.60:8080;<br \/>\nserver 192.168.1.62:8080;<br \/>\nserver 192.168.0.63:8080;<br \/>\n}<\/p>\n<p>Modify default.conf: vi \/etc\/nginx\/conf.d\/default.conf, amend as follows:<br \/>\nvi \/etc\/nginx\/conf.d\/default.conf<br \/>\nneed to amend the below lines<br \/>\n#location \/ {<br \/>\n# root \/usr\/share\/nginx\/html;<br \/>\n# index index.html index.htm;<br \/>\n#}<\/p>\n<p># new configuration default forwards the request to tomcats. conf configuration upstream processing<br \/>\nlocation \/ {<br \/>\nproxy_set_header Host $host;<br \/>\nproxy_set_header X-Real-IP $remote_addr;<br \/>\nproxy_set_header REMOTE-HOST $remote_addr;<br \/>\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br \/>\nproxy_pass http:\/\/tomcats;<br \/>\n}<\/p>\n<p>After saving reload the configuration: nginx -s reload<\/p>\n<p>Four separate static resource configuration<\/p>\n<p>Modify default.conf: vi \/etc\/nginx\/conf.d\/default.conf, add the following configuration:<br \/>\nvi \/etc\/nginx\/conf.d\/default.conf<\/p>\n<p>All js, css requests related static resource files processed by Nginx<\/p>\n<p>location ~.*\\.(js|css)$ {<br \/>\nroot \/opt\/static-resources;<br \/>\nexpires 12h;<br \/>\n}<\/p>\n<p>Request # All photos and other multimedia-related static resource files is handled by Nginx<\/p>\n<p>location ~.*\\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {<br \/>\nroot \/opt\/static-resources;<br \/>\nexpires 7d;<br \/>\n}<\/p>\n<p>Create a Directory for the Certificate<br \/>\nmkdir \/etc\/nginx\/ssl<br \/>\ncd \/etc\/nginx\/ssl<br \/>\nopenssl genrsa -des3 -out server.key 2048<br \/>\nopenssl req -new -key server.key -out server.csr<br \/>\ncp server.key server.key.org<br \/>\nopenssl rsa -in server.key.org -out server.key<br \/>\nopenssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt<\/p>\n<p>server {<br \/>\nlisten 80;<br \/>\nlisten 443 default ssl;<br \/>\nserver_name cluster1.rmohan.com;<br \/>\nkeepalive_timeout 70;<br \/>\n# ssl on;<br \/>\nssl_certificate \/etc\/nginx\/ssl\/server.crt;<br \/>\nssl_certificate_key \/etc\/nginx\/ssl\/server.key;<br \/>\nssl_protocols TLSv1 TLSv1.1 TLSv1.2;<br \/>\nssl_ciphers HIGH:!aNULL:!MD5;<br \/>\nssl_prefer_server_ciphers on;<br \/>\n}<\/p>\n<p>Nginx server security configuration<\/p>\n<p>First, turn off SELinux<br \/>\nSecurity-Enhanced Linux (SELinux) is a Linux kernel feature that provides security policy protection mechanism supports access control.<br \/>\nHowever, SELinux brings additional security and the disproportionate use of complexity, cost is not high<\/p>\n<p>sed -i \/SELINUX=enforcing\/SELINUX=disabled\/ \/etc\/selinux\/config<\/p>\n<p>\/usr\/sbin\/sestatus -v # Check status<\/p>\n<p>Second, the least privilege allowed by zoning mount<\/p>\n<p>A separate partition on the server nginx directory.<\/p>\n<p>For example, create a new partition \/dev\/sda5 (first logical partition), and mounted at \/nginx.<br \/>\nMake sure \/nginx is noexec,nodev and nosetuid permission to mount<\/p>\n<p>The following is my \/etc\/fstab mount \/nginx information: LABEL=\/nginx \/nginx ext3 defaults,nosuid,noexec,nodev 1 2<\/p>\n<p>Note: You need to create a new partition using fdisk and mkfs.ext3 command.<br \/>\nThird, to strengthen the Linux security configuration \/etc\/sysctl.conf<\/p>\n<p>You can control and configure the Linux kernel by editing \/etc\/sysctl.conf, network settings<\/p>\n<p># Avoid a smurf attack<\/p>\n<p>net.ipv4.icmp_echo_ignore_broadcasts = 1<\/p>\n<p># Turn on protection for bad icmp error messages<\/p>\n<p>net.ipv4.icmp_ignore_bogus_error_responses = 1<\/p>\n<p># Turn on syncookies for SYN flood attack protection<\/p>\n<p>net.ipv4.tcp_syncookies = 1<\/p>\n<p># Turn on and log spoofed, source routed, and redirect packets<\/p>\n<p>net.ipv4.conf.all.log_martians = 1<\/p>\n<p>net.ipv4.conf.default.log_martians = 1<\/p>\n<p># No source routed packets here<\/p>\n<p>net.ipv4.conf.all.accept_source_route = 0<\/p>\n<p>net.ipv4.conf.default.accept_source_route = 0<\/p>\n<p># Turn on reverse path filtering<\/p>\n<p>net.ipv4.conf.all.rp_filter = 1<\/p>\n<p>net.ipv4.conf.default.rp_filter = 1<\/p>\n<p># Make sure no one can alter the routing tables<\/p>\n<p>net.ipv4.conf.all.accept_redirects = 0<\/p>\n<p>net.ipv4.conf.default.accept_redirects = 0<\/p>\n<p>net.ipv4.conf.all.secure_redirects = 0<\/p>\n<p>net.ipv4.conf.default.secure_redirects = 0<\/p>\n<p># Don\u2019t act as a router<\/p>\n<p>net.ipv4.ip_forward = 0<\/p>\n<p>net.ipv4.conf.all.send_redirects = 0<\/p>\n<p>net.ipv4.conf.default.send_redirects = 0<\/p>\n<p># Turn on execshild<\/p>\n<p>kernel.exec-shield = 1<\/p>\n<p>kernel.randomize_va_space = 1<\/p>\n<p># Tuen IPv6<\/p>\n<p>net.ipv6.conf.default.router_solicitations = 0<\/p>\n<p>net.ipv6.conf.default.accept_ra_rtr_pref = 0<\/p>\n<p>net.ipv6.conf.default.accept_ra_pinfo = 0<\/p>\n<p>net.ipv6.conf.default.accept_ra_defrtr = 0<\/p>\n<p>net.ipv6.conf.default.autoconf = 0<\/p>\n<p>net.ipv6.conf.default.dad_transmits = 0<\/p>\n<p>net.ipv6.conf.default.max_addresses = 1<\/p>\n<p># Optimization for port usefor LBs<\/p>\n<p># Increase system file descriptor limit<\/p>\n<p>fs.file-max = 65535<\/p>\n<p># Allow for more PIDs (to reduce rollover problems); may break some programs 32768<\/p>\n<p>kernel.pid_max = 65536<\/p>\n<p># Increase system IP port limits<\/p>\n<p>net.ipv4.ip_local_port_range = 2000 65000<\/p>\n<p># Increase TCP max buffer size setable using setsockopt()<\/p>\n<p>net.ipv4.tcp_rmem = 4096 87380 8388608<\/p>\n<p>net.ipv4.tcp_wmem = 4096 87380 8388608<\/p>\n<p># Increase Linux auto tuning TCP buffer limits<\/p>\n<p># min, default, and max number of bytes to use<\/p>\n<p># set max to at least 4MB, or higher if you use very high BDP paths<\/p>\n<p># Tcp Windows etc<\/p>\n<p>net.core.rmem_max = 8388608<\/p>\n<p>net.core.wmem_max = 8388608<\/p>\n<p>net.core.netdev_max_backlog = 5000<\/p>\n<p>net.ipv4.tcp_window_scaling = 1<\/p>\n<p>Fourth, remove all unnecessary Nginx module<\/p>\n<p>You need to make the number of modules directly by compiling the source code Nginx minimized. By limiting access to only allow web server module to minimize risk.<br \/>\nYou can configure only install nginx modules you need. For example, disabling SSL and autoindex module you can execute the following command:<\/p>\n<p>.\/configure -without-http_autoindex_module -without-http_ssi_module<br \/>\nmake &amp;&amp; make install<\/p>\n<p>Change nginx version name, edit the file \/h\/http\/ngx_http_header_filter_module.c?<\/p>\n<p>vim src\/http\/ngx_http_header_filter_module.c<\/p>\n<p>static char ngx_http_server_string[] = \u201cServer: nginx\u201d CRLF;<\/p>\n<p>static char ngx_http_server_full_string[] = \u201cServer: \u201d NGINX_VER CRLF;<\/p>\n<p>\/\/change to<\/p>\n<p>static char ngx_http_server_string[] = \u201cServer: Mohan Web Server\u201d CRLF;<\/p>\n<p>static char ngx_http_server_full_string[] = \u201cServer: Mohan Web Server\u201d CRLF;<\/p>\n<p>Close nginx version number display<\/p>\n<p>server_tokens off<\/p>\n<p>Fifth, based Iptables firewall restrictions<\/p>\n<p>The following firewall script block any addition to allowing:<\/p>\n<p>HTTP (TCP port 80) of a request from<br \/>\nICMP ping requests from<br \/>\nntp (port 123) requests output<br \/>\nsmtp (TCP port 25) request output<\/p>\n<p>Six control buffer overflow attacks<\/p>\n<p>Edit and set all clients buffer size limit is as follows:<\/p>\n<p>client_body_buffer_size 1K;<\/p>\n<p>client_header_buffer_size 1k;<\/p>\n<p>client_max_body_size 1k;<\/p>\n<p>large_client_header_buffers 2 1k;<\/p>\n<p>client_body_buffer_size 1k (default 8k or 16k) This instruction can specify the buffer size of the connection request entity.<br \/>\nIf the value exceeds the specified buffer connection request, then the whole or part of the requesting entity will try to write a temporary file.<br \/>\nclient_header_buffer_size 1k directive specifies the client request buffer size of the head.<br \/>\nIn most cases a request header is not greater than 1k, but if there is a large cookie wap from the client that it may be greater than 1k,<br \/>\nNginx will assign it a larger buffer, this value can be set inside the large_client_header_buffers .<br \/>\nclient_max_body_size 1k- directive specifies the maximum allowable size of the client requesting entity connected, it appears in the Content-Length header field of the request.<\/p>\n<p>If the request is greater than the specified value, the client will receive a &#8220;Request Entity Too Large&#8221; (413) error. Remember, the browser does not know how to display the error.<br \/>\nlarge_client_header_buffers- specify the client number and size of some of the larger buffer request header use.<br \/>\nRequest a field can not be greater than the buffer size, if the client sends a relatively large head, nginx returns &#8220;Request URI too large&#8221; (414)<br \/>\nSimilarly, the head of the longest field of the request can not be greater than one buffer, otherwise the server will return &#8220;Bad request&#8221; (400). Separate buffer only when demand.<br \/>\nThe default buffer size for the operating system paging file size is usually 4k or 8k, if a connection request is ultimately state to keep- alive, it occupied the buffer will be freed.<\/p>\n<p>You also need to improve server performance control timeouts and disconnects the client. Edit as follows:<\/p>\n<p>client_body_timeout 10;<br \/>\nclient_header_timeout 10;<br \/>\nkeepalive_timeout 5 5;<br \/>\nsend_timeout 10;<\/p>\n<p>\u2022 client_body_timeout 10; &#8211; directive specifies the timeout request entity read. Here timeout refers to a requesting entity did not enter the reading step, if the connection after this time the client does not have any response, Nginx will return a &#8220;Request time out&#8221; (408) error.<br \/>\n\u2022 client_header_timeout 10; &#8211; directive specifies the client request header headline read timeout. Here timeout refers to a request header did not enter the reading step, if the connection after this time the client does not have any response, Nginx will return a &#8220;Request time out&#8221; (408) error.<br \/>\n\u2022 keepalive_timeout 5 5; &#8211; the first parameter specifies the timeout length of the client and server connections, over this time, the server will close the connection. The second parameter (optional) specifies the response header Keep-Alive: timeout = time value time, this value can make some browsers know when to close the connection to the server not repeat off if you do not specify this parameter , nginx does not send Keep-Alive header information in the response. (This does not refer to how a connection &#8220;Keep-Alive&#8221;) These two values ??of the parameters can be different.<br \/>\n\u2022 send_timeout 10; directive specifies the timeout is sent to the client after the response, Timeout refers not enter a complete state established, completed only two handshakes, more than this time if the client does not have any response, nginx will close the connection.<\/p>\n<p>Seven control concurrent connections<\/p>\n<p>You can use NginxHttpLimitZone module to restrict a specific session or a special case of concurrent connections IP addresses under. Edit nginx.conf:<\/p>\n<p>### Directive describes the zone, in which the session states are stored i.e. store in slimits. ###<\/p>\n<p>### 1m can handle 32000 sessions with 32 bytes\/session, set to 5m x 32000 session ###<\/p>\n<p>limit_zone slimits $binary_remote_addr 5m;<\/p>\n<p>### Control maximum number of simultaneous connections for one session i.e. ###<\/p>\n<p>### restricts the amount of connections from a single ip address ###<\/p>\n<p>limit_conn slimits 5<\/p>\n<p>The above represents the remote IP address to limit each client connection can not be open at the same time more than five.<\/p>\n<p>Eight, only allow access to our domain<\/p>\n<p>If the robot is just random scan all domain name servers, that reject the request. You must allow the configuration of the virtual domain or reverse proxy request. You do not use IP addresses to reject.<\/p>\n<p>if ($host !~ ^(test.in|www.test.in|images.test.in)$ ) {<br \/>\nreturn 444;<br \/>\n}<\/p>\n<p>Nine, to limit the request method available<\/p>\n<p>GET and POST are the Internet&#8217;s most commonly used method. The method of the Web server is defined in RFC 2616. If the Web server is not required to run all available methods, they should be disabled. The following command will filter only allows GET, HEAD and POST methods:<\/p>\n<p>## Only allow these request methods ##<\/p>\n<p>if ($request_method !~ ^(GET|HEAD|POST)$ ) {<\/p>\n<p>return 444;<\/p>\n<p>}<\/p>\n<p>## Do not accept DELETE, SEARCH and other methods ##<\/p>\n<p>More about HTTP method introduced<\/p>\n<p>\u2022 GET method is used to request,<\/p>\n<p>\u2022 HEAD method is the same, unless GET request to the server can not return the message body.<\/p>\n<p>\u2022 POST method can involve many things, such as storage or update data, or ordering products, or send e-mail by submitting the form. This is usually the use of server-side processing, such as PHP, Perl and Python scripts. If the file you want to upload and server processing the data, you must use this method.<\/p>\n<p>Ten, how to refuse a number of User-Agents?<\/p>\n<p>You can easily stop User-Agents, such as scanners, robotics and abuse your server spammers.<\/p>\n<p>## Block download agents ##<\/p>\n<p>if ($http_user_agent ~* LWP::Simple|BBBike|wget) {<\/p>\n<p>return 403;<\/p>\n<p>}<\/p>\n<p>Soso and the proper way to prevent robots:<\/p>\n<p>## Block some robots ##<\/p>\n<p>if ($http_user_agent ~* Sosospider|YodaoBot) {<\/p>\n<p>return 403;<\/p>\n<p>}<\/p>\n<p>XI prevent image hotlinking<\/p>\n<p>Pictures or HTML Daolian mean someone directly with your website address to display pictures on his website. The end result, you need to pay the extra cost of broadband. This is often in the forum and blog. I strongly recommend that you block and prevent hotlinking behavior.<\/p>\n<p># Stop deep linking or hot linking<\/p>\n<p>location \/images\/ {<\/p>\n<p>valid_referers none blocked www.example.com example.com;<\/p>\n<p>if ($invalid_referer) {<\/p>\n<p>return 403;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>For example: the redirect and display the specified image<\/p>\n<p>valid_referers blocked www.example.com example.com;<\/p>\n<p>valid_referers blocked www.example.com example.com;<\/p>\n<p>if ($invalid_referer) {<\/p>\n<p>rewrite ^\/images\/uploads.*\\.(gif|jpg|jpeg|png)$ http:\/\/www.examples.com\/banned.jpg last<\/p>\n<p>}<\/p>\n<p>Twelve, directory restrictions<\/p>\n<p>You can set access permissions on the specified directory. All websites directory should one configuration, allowing only access to the directory.<br \/>\nAccess by IP address restrictions<br \/>\nYou can restrict access by IP address directory \/ admin \/:<\/p>\n<p>ocation \/docs\/ {<\/p>\n<p>## block one workstation<\/p>\n<p>deny 192.168.1.1;<\/p>\n<p>## allow anyone in 192.168.1.0\/24<\/p>\n<p>allow 192.168.1.0\/24;<\/p>\n<p>## drop rest of the world<\/p>\n<p>deny all;<\/p>\n<p>}<\/p>\n<p>Via password protected directory, first create the password file and increase the &#8220;user&#8221; user<\/p>\n<p>mkdir \/usr\/local\/nginx\/conf\/.htpasswd\/<\/p>\n<p>htpasswd -c \/usr\/local\/nginx\/conf\/.htpasswd\/passwd user<\/p>\n<p>Edit nginx.conf, added need protected directories<\/p>\n<p>### Password Protect \/personal-images\/ and \/delta\/ directories ###<\/p>\n<p>location ~ \/(personal-images\/.*|delta\/.*) {<\/p>\n<p>auth_basic \u201cRestricted\u201d;<\/p>\n<p>auth_basic_user_file \/usr\/local\/nginx\/conf\/.htpasswd\/passwd;<\/p>\n<p>}<\/p>\n<p>Once the password file has been generated, you can also use the following command to allow access to the user increases<\/p>\n<p>htpasswd -s \/usr\/local\/nginx\/conf\/.htpasswd\/passwd userName<\/p>\n<p>Thirteen, Nginx SSL Configuration<\/p>\n<p>HTTP is a plain text protocol, which is open to passive surveillance. You should use SSL to encrypt your user content.<br \/>\nCreate SSL certificate, execute the following command:<\/p>\n<p>cd \/usr\/local\/nginx\/conf<\/p>\n<p>openssl genrsa -des3 -out server.key 1024<\/p>\n<p>openssl req -new -key server.key -out server.csr<\/p>\n<p>cp server.key server.key.org<\/p>\n<p>openssl rsa -in server.key.org -out server.key<\/p>\n<p>openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt<\/p>\n<p>Edit nginx.conf press following updates:<\/p>\n<p>server {<\/p>\n<p>server_name example.com;<\/p>\n<p>listen 443;<\/p>\n<p>ssl on;<\/p>\n<p>ssl_certificate \/usr\/local\/nginx\/conf\/server.crt;<\/p>\n<p>ssl_certificate_key \/usr\/local\/nginx\/conf\/server.key;<\/p>\n<p>access_log \/usr\/local\/nginx\/logs\/ssl.access.log;<\/p>\n<p>error_log \/usr\/local\/nginx\/logs\/ssl.error.log;<\/p>\n<p>}<\/p>\n<p>Fourteen, Nginx and PHP Security Recommendations<\/p>\n<p>PHP is a popular scripting language on the server side. Edit \/etc\/php.ini file as follows:<\/p>\n<p># Disallow dangerous functions<\/p>\n<p>disable_functions = phpinfo, system, mail, exec<\/p>\n<p>## Try to limit resources ##<\/p>\n<p># Maximum execution time of each script, in seconds<\/p>\n<p>max_execution_time = 30<\/p>\n<p># Maximum amount of time each script may spend parsing request data<\/p>\n<p>max_input_time = 60<\/p>\n<p># Maximum amount of memory a script may consume (8MB)<\/p>\n<p>memory_limit = 8M<\/p>\n<p># Maximum size of POST data that PHP will accept.<\/p>\n<p>post_max_size = 8M<\/p>\n<p># Whether to allow HTTP file uploads.<\/p>\n<p>file_uploads = Off<\/p>\n<p># Maximum allowed size for uploaded files.<\/p>\n<p>upload_max_filesize = 2M<\/p>\n<p># Do not expose PHP error messages to external users<\/p>\n<p>display_errors = Off<\/p>\n<p># Turn on safe mode<\/p>\n<p>safe_mode = On<\/p>\n<p># Only allow access to executables in isolated directory<\/p>\n<p>safe_mode_exec_dir = php-required-executables-path<\/p>\n<p># Limit external access to PHP environment<\/p>\n<p>safe_mode_allowed_env_vars = PHP_<\/p>\n<p># Restrict PHP information leakage<\/p>\n<p>expose_php = Off<\/p>\n<p># Log all errors<\/p>\n<p>log_errors = On<\/p>\n<p># Do not register globals for input data<\/p>\n<p>register_globals = Off<\/p>\n<p># Minimize allowable PHP post size<\/p>\n<p>post_max_size = 1K<\/p>\n<p># Ensure PHP redirects appropriately<\/p>\n<p>cgi.force_redirect = 0<\/p>\n<p># Disallow uploading unless necessary<\/p>\n<p># Enable SQL safe mode<\/p>\n<p>sql.safe_mode = On<\/p>\n<p># Avoid Opening remote files<\/p>\n<p>allow_url_fopen = Off<\/p>\n<p>Fifth, if possible, let Nginx run in a chroot jail<\/p>\n<p>The nginx placed in a chroot jail to reduce the potential for illegal entry into other directories. You can use the traditional and nginx installed with chroot. If possible, that use FreeBSD jails, Xen, OpenVZ virtualization container concept.<\/p>\n<p>XVI firewall level limits the number of connections for each IP<\/p>\n<p>Network server must monitor connections and connection limits per second. PF and Iptales are able to enter your nginx server before the end user to block access.<br \/>\nLinux Iptables: limit the number of connections for each Nginx<br \/>\nfollowing example will prevent from a single IP connection of more than 15 the number of ports 80, 60 seconds.<\/p>\n<p>\/sbin\/iptables -A INPUT -p tcp \u2013dport 80 -i eth0 -m state \u2013state NEW -m recent \u2013set<\/p>\n<p>\/sbin\/iptables -A INPUT -p tcp \u2013dport 80 -i eth0 -m state \u2013state NEW -m recent \u2013update \u2013seconds 60 \u2013hitcount 15 -j DROP<\/p>\n<p>service iptables save<\/p>\n<p>According to your specific situation to set the connection limit.<\/p>\n<p>XVII configure the operating system to protect Web servers<\/p>\n<p>Like the above described start SELinux Correct set permissions \/nginx document root directory.<br \/>\nNginx running in user nginx. But the root directory (\/ nginx or \/usr\/local\/nginx\/html\/) should not be set, or the user belongs to the user nginx nginx writable.<br \/>\nFind the error file permissions can use the following command:<\/p>\n<p>find \/nginx -user nginx<\/p>\n<p>find \/usr\/local\/nginx\/html -user nginx<\/p>\n<p>Make sure you are more ownership of the root or other users, a typical permission settings \/usr\/local\/nginx\/html\/<\/p>\n<p>ls -l \/usr\/local\/nginx\/html\/<\/p>\n<p>Sample output:<\/p>\n<p>-rw-r-r- 1 root root 925 Jan 3 00:50 error4xx.html<\/p>\n<p>-rw-r-r- 1 root root 52 Jan 3 10:00 error5xx.html<\/p>\n<p>-rw-r-r- 1 root root 134 Jan 3 00:52 index.html<\/p>\n<p>You must delete the backup files from the vi or another text editor to create:<\/p>\n<p>find \/nginx -name \u2018.?*\u2019 -not -name .ht* -or -name \u2018*~\u2019 -or -name \u2018*.bak*\u2019 -or -name \u2018*.old*\u2019<\/p>\n<p>find \/usr\/local\/nginx\/html\/ -name \u2018.?*\u2019 -not -name .ht* -or -name \u2018*~\u2019 -or -name \u2018*.bak*\u2019 -or -name \u2018*.old*\u2019<\/p>\n<p>To delete these files by -delete option to find command.<\/p>\n<p>Eighth, the outgoing connections limit Nginx<\/p>\n<p>Hackers can use tools such as wget download your local file server. Iptables from using nginx user to block outgoing connections. ipt_owner module tries to match the creator of locally generated packets. The following example allows only users 80 user connections outside.<\/p>\n<p>\/sbin\/iptables -A OUTPUT -o eth0 -m owner \u2013uid-owner vivek -p tcp \u2013dport 80 -m state \u2013state NEW,ESTABLISHED -j ACCEPT<\/p>\n<p>With the above configuration, your nginx server is already very safe and you can publish web pages. However, you should also find more information on security settings according to your site procedures. For example, wordpress or a third-party program.<\/p>\n<p>nginx is a good web server, providing a full range of speed limit function, the main function module is ngx_http_core_module,<br \/>\nngx_http_limit_conn_module and ngx_http_limit_req_module, the first module in limit_rate function (limited speed bandwidth),<\/p>\n<p>the latter two modules Literally , functions are limiting connections (limit connection) and restriction request (limit request), these modules are compiled into the default nginx core.<\/p>\n<p>All limits are for IP and therefore CC, DDOS has some defensive role.<\/p>\n<p>Limited bandwidth is very easy to understand, directly on the example<\/p>\n<p>location \/mp3 {<br \/>\nlimit_rate 200k;<br \/>\n}<\/p>\n<p>There is a way you can make the speed limit is more humane, namely transmission speed after the start of a certain flow,<\/p>\n<p>Such as the first full-speed transmission 1M, then start speed:<\/p>\n<p>location \/photo {<br \/>\nlimit_rate_after 1m;<br \/>\nlimit_rate 100k;<br \/>\n}<\/p>\n<p>Then speak and limit the number of concurrent requests.<\/p>\n<p>Why do these two modules? Because we know that a page is usually more than one child module, such as five pictures, then we request this page initiated a connection,<br \/>\nbut this is a connection request that contains the five pictures, which means that a connection can initiate multiple requests . We have to maintain the user experience,<br \/>\nit is to limit the number of connections or requests, to be selected according to actual needs.<\/p>\n<p>limit the number of connections<\/p>\n<p>To restrict access, you must first have a container for connection count, add the following code segment http:<\/p>\n<p>limit_conn_zone $binary_remote_addr zone=addr:5m;<\/p>\n<p>This will create a 5M in memory size, speed pool named addr (each connection occupies 32 or 64 bytes, 5m size which can accommodate tens of thousands of connections, is usually sufficient,<br \/>\nif memory is exhausted 5M , will return to 503)<\/p>\n<p>Next, the need for a different location server (location above) to limit the rate, such as restrictions on the number of concurrent connections per IP is 2,<\/p>\n<p>limit_conn addr 2;<\/p>\n<p>2, limit the number of requests<\/p>\n<p>To limit the number of requests, you must first create a speed pool, add the following code segment at http:<\/p>\n<p>limit_conn_zone $binary_remote_addr zone=addr:5m;<\/p>\n<p>Limit divided into global and local speed limit,<\/p>\n<p>For global speed limit, we only need to be followed by the parameters, such as 20 requests per second, rate = 20r \/ s, namely:<\/p>\n<p>limit_req_zone $binary_remote_addr zone=perip:5m rate=20r\/s;<\/p>\n<p>Sometimes we want to adjust the location segment links, you can help burst parameters<\/p>\n<p>limit_req zone=one burst=50;<\/p>\n<p>If you do not want to delay, there nodelay parameters<\/p>\n<p>limit_req zone=one burst=50 nodelay;<\/p>\n<p>The above is the rate-limiting nginx Introduction, inappropriate, please correct me. As for the specific use of methods which limit must be considered, so as not to damage the user experience.<\/p>\n<p>nginx log filter Web Crawler<\/p>\n<p>Nginx log analysis, when there is a headache many spiders reptiles marks.<\/p>\n<p>Given that most spiders reptiles are called xx-bot or xx-spider, the following methods can be written to a separate log reptiles:<\/p>\n<p>location \/ {<br \/>\nif ($http_user_agent ~* &#8220;bot|spider&#8221;) {<br \/>\naccess_log \/var\/log\/nginx\/spider.access.log;<br \/>\n}<br \/>\n}<br \/>\nOr simply do not write log<\/p>\n<p>location \/ {<br \/>\nif ($http_user_agent ~* &#8220;bot|spider&#8221;) {<br \/>\naccess_log off;<br \/>\n}<br \/>\n}<\/p>\n<p>Tomcat implement multi-instance use systemd centos 7 RHEL 7<\/p>\n<p>rpm -ivh jdk-8u60-linux-x64.rpm<\/p>\n<p>getent group tomcat || groupadd -r tomcat<br \/>\ngetent passwd tomcat || useradd -r -d \/opt -s \/bin\/nologin tomcat<\/p>\n<p>cd \/opt<br \/>\nwget http:\/\/mirror.nus.edu.sg\/apache\/tomcat\/tomcat-8\/v8.0.30\/bin\/apache-tomcat-8.0.30.tar.gz<br \/>\ntar xzf jdk-8u40-linux-i586.tar.gz<\/p>\n<p>mv apache-tomcat-8.0.30 tomcat01<br \/>\nchown -R tomcat:tomcat tomcat01<\/p>\n<p>tar zxvf apache-tomcat-8.0.30.tar.gz<br \/>\nmv apache-tomcat-8.0.30 tomcat02<br \/>\nchown -R tomcat:tomcat tomcat02<\/p>\n<p>sed -i &#8216;s\/8080\/8081\/g&#8217; \/opt\/tomcat01\/conf\/server.xml<br \/>\nsed -i &#8216;s\/8005\/8001\/g&#8217; \/opt\/tomcat01\/conf\/server.xml<br \/>\nsed -i &#8216;s\/8080\/8082\/g&#8217; \/opt\/tomcat02\/conf\/server.xml<br \/>\nsed -i &#8216;s\/8005\/8002\/g&#8217; \/opt\/tomcat02\/conf\/server.xml<\/p>\n<p>sed -i &#8216;\/8009\/d&#8217; \/opt\/tomcat01\/conf\/server.xml<br \/>\nsed -i &#8216;\/8009\/d&#8217; \/opt\/tomcat01\/conf\/server.xml<\/p>\n<p>cd \/usr\/lib\/systemd\/system<br \/>\ncat &gt;tomcat01.service &lt;&lt;EOF<br \/>\n[Unit]<br \/>\nDescription=Apache Tomcat 7<br \/>\nAfter=network.target<br \/>\n[Service]<br \/>\nType=oneshot<br \/>\nExecStart=\/opt\/tomcat01\/bin\/startup.sh<br \/>\nExecStop=\/opt\/tomcat01\/bin\/shutdown.sh<br \/>\nRemainAfterExit=yes<br \/>\nUser=tomcat<br \/>\nGroup=tomcat<br \/>\n[Install]<br \/>\nWantedBy=multi-user.target<br \/>\nEOF<\/p>\n<p>sed &#8216;s\/tomcat01\/tomcat02\/g&#8217; tomcat01.service &gt; tomcat02.service<\/p>\n<p>systemctl enable tomcat01<br \/>\nsystemctl enable tomcat02<br \/>\nsystemctl start tomcat01<br \/>\nsystemctl start tomcat02<\/p>\n<p>proxy_cache_path \/var\/cache\/nginx\/proxy_cache levels=1:2 keys_zone=static:10m inactive=30d max_size=1g;<\/p>\n<p>upstream tomcat {<br \/>\nip_hash ;<br \/>\n#hash $remote_addr consistent;<br \/>\nserver 127.0.0.1:8081 max_fails=1 fail_timeout=2s ;<br \/>\nserver 127.0.0.1:8082 max_fails=1 fail_timeout=2s ;<br \/>\nkeepalive 16;<br \/>\n}<\/p>\n<p>server {<br \/>\nlisten 80;<br \/>\nserver_name tomcat.example.com;<\/p>\n<p>charset utf-8;<br \/>\naccess_log \/var\/log\/nginx\/tomcat.access.log main;<br \/>\nroot \/usr\/share\/nginx\/html;<br \/>\nindex index.html index.htm index.jsp;<\/p>\n<p>location \/ {<br \/>\nproxy_pass http:\/\/tomcat;<br \/>\nproxy_redirect off;<br \/>\nproxy_set_header Host $host;<br \/>\nproxy_set_header X-Real-IP $remote_addr;<br \/>\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br \/>\nproxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;<\/p>\n<p>proxy_connect_timeout 300;<br \/>\nproxy_send_timeout 300;<br \/>\nproxy_read_timeout 300;<br \/>\nproxy_http_version 1.1;<br \/>\nproxy_set_header Connection &#8220;&#8221;;<\/p>\n<p>add_header X-Backend &#8220;$upstream_addr&#8221;;<br \/>\n}<\/p>\n<p>location ~* ^.+\\.(js|css|ico|gif|jpg|jpeg|png)$ {<br \/>\nproxy_pass http:\/\/tomcat ;<br \/>\nproxy_redirect off;<br \/>\nproxy_set_header Host $host;<br \/>\nproxy_set_header X-Real-IP $remote_addr;<br \/>\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<br \/>\nproxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;<\/p>\n<p>proxy_connect_timeout 300;<br \/>\nproxy_send_timeout 300;<br \/>\nproxy_read_timeout 300;<br \/>\nproxy_http_version 1.1;<br \/>\nproxy_set_header Connection &#8220;&#8221;;<\/p>\n<p>proxy_cache static;<br \/>\nproxy_cache_key $host$uri$is_args$args;<br \/>\nproxy_cache_valid 200 302 7d;<br \/>\nproxy_cache_valid 404 1m;<br \/>\nproxy_cache_valid any 1h;<br \/>\nadd_header X-Cache $upstream_cache_status;<\/p>\n<p>#log_not_found off;<br \/>\n#access_log off;<br \/>\nexpires max;<br \/>\n}<\/p>\n<p>location ~ \/\\.ht {<br \/>\ndeny all;<br \/>\n}<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nginx is a high performance web server software. It is a much more flexible and lightweight program than apache.<\/p>\n<p>yum install epel-release<\/p>\n<p>yum install nginx<\/p>\n<p>ifconfig eth0 | grep inet | awk &#8216;{ print $2 }&#8217;<\/p>\n<p>wget \u2013no-cookies \u2013no-check-certificate \u2013header \u201cCookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie\u201d \u201chttp:\/\/download.oracle.com\/otn-pub\/java\/jdk\/8u60-b27\/jdk-8u60-linux-x64.tar.gz\u201d wget http:\/\/mirror.nus.edu.sg\/apache\/tomcat\/tomcat-8\/v8.0.30\/bin\/apache-tomcat-8.0.30.tar.gz tar xzf jdk-8u40-linux-i586.tar.gz mkdir \/usr\/java\/<\/p>\n<p>cd \/usr\/java\/jdk1.8.0_40\/ [root@cluster1 java]# [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7295"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7295"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7295\/revisions"}],"predecessor-version":[{"id":7296,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7295\/revisions\/7296"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}