Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Nginx Optimization and hotlink Protection

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Nginx is a very lightweight HTTP server written by Russians, Nginx, pronounced "engine X", is a high

Performance of HTTP and reverse proxy server, is also an IMAP/POP3/SMTP proxy server. Nginx is provided by

Russian Igor Sysoev developed for the second most visited Rambler.ru site in Russia.

Nginx is written in an event-driven (epoll) way, so it has very good performance and is also a very efficient inverse.

To agents, load balancing. However, Nginx does not support cgi mode, because it can reduce some of the programs it brings.

A loophole in the order. So you must use FastCGI to execute the PHP program.

Due to some advantages of Nginx itself, lightweight, open source and easy to use, more and more companies are using nginx as their own company.

Web application server, this article describes in detail the installation of nginx source code and optimizes the configuration of nginx.

I. Optimization of Nginx

1. Optimization before compilation and installation

Pre-compilation optimization is mainly used to modify the program name and so on, in order to change the source code hiding software name and version number.

Install dependent packages such as zlib-devel, pcre-devel, etc.

[root@bogon ~] # [root@bogon ~] # tar zxf nginx-1.14.0.tar.gz [root@bogon ~] # cd nginx-1.14.0/ [root@bogon nginx-1.14.0] # yum-y install openssl-devel pcre-devel

two。 Compile and install nginx

. / configure-- prefix=/usr/local/nginx1.14-- with-http_dav_module-- with-http_stub_status_module-- with-http_addition_module-- with-http_sub_module-- with-http_flv_module-- with-http_mp4_module-- with-pcre-- with-http_ssl_module-- with-http_gzip_static_module-- user=nginx-- group=nginxmake & & make install

3. Create the required users

[root@bogon nginx-1.14.0] # useradd nginx- s / sbin/nologin-M

4. Link command directory

[root@bogon nginx-1.14.0] # ln-s / usr/local/nginx1.14/sbin/nginx / usr/local/sbin/

5. Open nginx and view the port

Nginx

[root@bogon nginx-1.14.0] # netstat-anpt | grep 80

Tcp 0 0 0.0.0 0 master 80 0.0.0 0. 0. 0 master

6. Modify / usr/local/nginx1.14/conf/nginx.conf

7. Modify / etc/security/limits.conf

8. Check to see if it hasn't changed.

9. Log out and check again to see if it has changed.

10. Open nginx and check the changes of nginx

11. Visit nginx on the page

2. Deploy LNMP (deployment environment requirements: nginx and php are in one, and then another apache is enabled for hotlink protection)

1. Install php

Install libmcrypt and dependency packages first

Compile and install php

/ configure-- prefix=/usr/local/php5.6-- with-mysql=mysqlnd-- with-pdo-mysql=mysqlnd-- with-mysqli=mysqlnd-- with-openssl-- enable-fpm-- enable-sockets-- enable-sysvshm-- enable-mbstring-- with-freetype-dir-- with-jpeg-dir-- with-png-dir-- with-zlib-- with-libxml-dir=/usr-- enable-xml-- with-mhash-- with-mcrypt=/usr/local/ Libmcrypt-with-config-file-path=/etc-with-config-file-scan-dir=/etc/php.d-with-bz2-enable-maintainer-ztsmake & & make intall

Provide php profile

[root@bogon php-5.6.27] # cp php.ini-production / etc/php.ini

Provide scripts for php-fpm

Provide php-fpm configuration file and edit

Modify / usr/local/php5.6/etc/php-fpm.conf

Start php to view port 9000

/ etc/init.d/php-fpm start [root@bogon etc] # netstat-anpt | grep 9000tcp 00 0.0.0.0 netstat 9000 0.0.0.0 LISTEN

Modify / usr/local/nginx1.14/conf/nginx.conf

Create / usr/local/nginx1.14/html/index.php

test

Kernel parameter optimization add / etc//sysctl.conffs.file-max = 999999net.ipv4.ip_forward = 0net.ipv4.conf.default.rp_filter = 1net.ipv4.conf.default.accept_source_route = 0kernel.sysrq = 0kernel.core_uses_pid = 1net.ipv4.tcp_syncookies = 1kernel.msgmnb = 65536kernel.msgmax = 65536kernel.shmmax = 68719476736kernel.shmall = 4294967296net.ipv4.tcp_max_tw_buckets = 6000net.ipv4.tcp_sack = 1net.ipv4.tcpwindows _ Scaling = 1net.ipv4.tcp_rmem = 10240 87380 12582912net.ipv4.tcp_wmem = 10240 87380 12582912net.core.wmem_default = 8388608net.core.rmem_default = 8388608net.core.rmem_max = 16777216net.core.wmem_max = 16777216net.core.netdev_max_backlog = 262144net.core.somaxconn = 40960net.ipv4.tcp_max_orphans = 3276800net.ipv4.tcp_max_syn_backlog = 262144net.ipv4.tcp_timestamps = 0net.ipv4.tcp_synack_retries = 1net.ipv4.tcp_syn_retries = 1net.ipv4.tcp_tw_recycle = 1net.ipv4.tcp_tw_reuse = 1net.ipv4.tcp_mem = 94500000 915000000 927000000net.ipv4.tcp_fin_timeout = 1net.ipv4.tcp_keepalive_time = 30net.ipv4.ip_local_port_range = 1024 65 000 execute sysctl-p to make the kernel changes take effect

View update valu

Modify / usr/local/nginx1.14/conf/nginx.conf

Sendfile on;tcp_nopush on;keepalive_timeout 65 / tcphands no delay on;client_header_buffer_size 4k / openopenings cache max=102400 inactive=20s;open_file_cache_valid 30s / openings fileholders cachepieces minuses 1 / clientships headerplates headerout 15 clientpieces headerplates timeout bodyweights timeout 15 / times timedoutconnection on;send_timeout 15 / off;client_max_body_size 10m fastcgists connecttimeout 600 fastcgivetimeouts timeout 600 fastcgivetimeouts readout 600 ways fastcgibles buffersize 64kitfastcgicards buffers 4 64k Fastcgi_busy_buffers_size 128k / usr/local/nginx1.10/nginx_tmp;fastcgi_intercept_errors on;fastcgi_cache_path / usr/local/nginx1.10/fastcgi_cache levels=1:2keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;gzip on;gzip_min_length 2k / gzipboxes buffers 4 32k / gzipstones httpboxes version 1.1 / gzipstones compounding level 6 Gzip_types text/plain text/css text/javascript application/json application/javascriptapplication/x-javascript application/xml;gzip_vary on;gzip_proxied anylocation * ^. +\. (jpg | gif | png | swf | flv | wma | wmv | asf | mp3 | mmf | zip | rar) ${valid_referers none blocked 192.168.1.50 If ($invalid_referer) {# return 302 http://www.benet.com/img/nolink.jpg; return 404; break;} access_log off;} location / {root html; index index.php index.html index.htm } location / status {stub_status on;} comment out the previous php test page

Location ~. *\. (php | php5)? ${root html;fastcgi_pass 127.0.0.1 rig 9000 fastcgival index index.php;include fastcgi.conf;fastcgi_cache cache_fastcgi;fastcgi_cache_valid 200 302 1h fastcgival cachets valid 301 d fastcgival cachems valid any 1m fastcgival cachems valid fastcheals http://$host$request_uri;} uses 1fastcgival cachems

Restart php

Nginx-s reload

test

Third, verification and stress testing

View the installation package for ab

Yum provides ab

Install httpd-tools

Yum-y install httpd-tools

Pressure testing

Ab-c 2000-n 10000 192.168.1.50/index.html

Ab-c 2000-n 10000 192.168.1.50/index.php

Compare the two stress tests

Verify hotlink protection

Use apache as a test site with the domain name www.test.com, make a hyperlink on the test page and link nginx

Create vim / var/www/html/index.html on the apache host

Turn on httpd

Systemctl start httpd

test

Forum

Php server unzips the package

Unzip Discuz_7.0.0_FULL_SC_UTF8.zip

Cd Discuz_7.0.0_FULL_SC_UTF8/

Ls

Ls upload/

Modify / etc/php.ini

Mysql server

Enter MySQL

Mysql-u root-p123

Create a database

Create database bbs

Grant all on. To root@'192.168.1.%' identified by '123.com'

Test browser enter http:// 's own IP address / bbs/install

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report