Linux Redhat 7 Nginx optimized configuration

Source: Internet
Author: User
Tags fpm nginx server

Nginx Basic Configuration

Nginx (engine x) is a Russian developed by an open-source web server software, Nginx can be used as a Web server support HTML and Php,nginx can also do reverse proxy, load balancing
Nginx using source package installation nginx-1.8.1.tar.gz
1. Install the dependency package with Yum first
yum - y install gcc pcre-devel openssl-devel zlib-devel make
2. Create a non-login user nginx, used as a starting Niginx user, to prevent the system caused by the software vulnerability to a greater loss.
Useradd-s/sbin/nologin Nginx
3. Unzip the TAR package, configure installation information, compile, install

    tar -xf nginx-1.8.1.tar.gz    cd nginx-1.8.1    ./configure --prefix=/usr/local/nginx --user=nginx  --group=nginx --with-http_ssl_module #prefix指定安装路径 --with-http_ssl_module是支持https安全网站的模块如果不需要可以不用安装    make&&make install

4. Do a soft link to the Nginx startup program, easy to manage nginx later
Ln-s/usr/local/nginx/sbin/nginx/usr/sbin/
5. Start/close Nginx
Start: Nginx #请注意重复启动会报错 80 port is occupied will be the error restart needs to be shut down before booting no restart
Close: Nginx-s stop
In the case of most modifications to the configuration file, without restarting Nginx, use the overloaded command to dynamically load the new configuration file in the case of normal service delivery
6.nginx as a Web server
General Choice Lnmp (Linux Nginx Mariadb Php) environment to build dynamic website
The package that needs to be installed is PHP php-mysql mariadb (Database client software) Mariabd-server Mariadb-devel PHP-FPM (the package needs to be downloaded, the software is a PHP spooler using 9000 port Nginx sends the client request PHP dynamic page to PHP-FPM, after PHP-FPM processing returns the result to the Nginx,nginx and returns the result to the client)

    yum -y install   mariadb   mariadb-server   mariadb-devel  php   php-mysql    rpm -hiv php-fpm-5.4.16-36.el7_1.x86_64.rpm   systemctl start php-fpm #启动php-fpm   systemctl status php-fpm  #查看php-fpm的运行状态   systemctl enable php-fpm #设置php-fpm开机自启

7. Modify the Nginx configuration file
The Nginx configuration file format is
http{
server{#定义虚拟机一个server是一个虚拟机
location{#网站目录 default is the HTML directory under the Nginx installation directory
}
}
}
Vim/usr/local/nginx/conf/nginx.conf
Partial interception of configuration files only
server {
Listen 80; #监听80端口默认http的服务端口
server_name localhost; #指定域名
CharSet Utf-8; #中文网站一般设置utf-8 encoding to avoid garbled
Location ~. php$ {#所有以php结尾的请求转到这个location处理
root html; #网站目录
Fastcgi_pass 127.0.0.1:9000; #请求转发给php-fpm Treatment
Fastcgi_index index.php; #默认首页
#fastcgi_param Script_filename/scripts$fastcgi_script_name;
Include fastcgi.conf; #倒入一个配置文件该文件都是提供一些变量参数
}
}
8. Heavy-duty Nginx
Nginx-s Reload
9. You can write a PHP file to test whether PHP can parse properly
vim/usr/local/nginx/html/test1.php
<?php
$i = "This is a test page";
echo $i;
?>
10. If the correct service is configured to start without error access localhost/test1.php will get a page that is a test page
11. Start the database service mariadb
Systemctl Start mariadb
12. Write a Database Test PHP page
vim/usr/local/nginx/html/test2.php
<?php
$links =mysql_connect ("localhost", "root", "password");
Note: Root is the MySQL account name, the password needs to be changed to the actual MySQL password, no password will be left blank
if ($links) {
echo "LINK db OK!!!";
}
else{
echo "LINK DB no!!!";
}
?>
13. Access localhost/test2.php will display the link db OK!!!

Nginx Optimization

1. Address Rewriting
Turn access a.html to b.html
Join in the Localtion
rewrite/a.html/b.html;
Transfer all access to a different domain
Join in server before localtion
Rewrite ^/http://www.tmooc.cn/;
Leave all access to other domain names to keep access to subdirectories unchanged
Join in server before localtion (use regular in this regular and shell are different \1 use $)
Rewrite ^/(. *) http://www.tmooc.cn/$1;
2. High concurrency
Nginx starts a process by default, 1024 concurrent
Worker_processes 4; #进程数和cpu核心数需要保持一致; 4-core CPU can start 4 processes

#error_log Logs/error.log;
#error_log Logs/error.log Notice;
#error_log Logs/error.log Info;

#pid Logs/nginx.pid;

Events {
Worker_connections 65535; #每个进程并发连接数默认1024 because a Linux system user can only open up to 1024 files by default, so you need to change the number of files that the Linux system can open by default after changing the number of nginx concurrency.
}
Modify Linux system settings all user software hardware open files up to a maximum of 100000 actually not that big.
Vim/etc/security/limits.conf
#<domain> <type> <item> <value>
#

    • Soft Nofile 100000 #
    • Hard Nofile 100000

3. Client-Side Caching
The general cache is cached when static content such as HTML CSS XML jpg png and so on
Add a location in the server to match the static file format to define the client cache
Location ~*. (Jpg|jpeg|gif|png|css|js|ico|xml) $ {
Expires 30d; Define client cache time of 30 days
}
4. Enable compression transmission, now all browsers support decompression, I open the Nginx server compression will reduce the size of the transmission content, but note that generally do not compress multimedia files such as songs, videos, pictures, and too small files; Because the compression process in the file will be added to the compressed information is too small file after compression will be larger, the multimedia file itself is compressed files and then compressed also when they add a layer of packaging in addition to the larger also consumes a lot of server resources
HTTP {
Server_tokens off; Do not display Nginx version number information to prevent hackers from using version vulnerabilities to launch an attack
Client_header_buffer_size 1k; Default request header information cache when the access address is too long 1k cache is not enough error, the customer experience is not good enough
Large_client_header_buffers 4 4k; The number and capacity of this large request packet header information exceeding 1k is adjusted to 4 4k collection 16k The normal case of requests will not exceed this size.
gzip on; Enable gzip compression
Gzip_min_length 1000; Files less than 1000 bytes are not compressed
Gzip_comp_level 4; Compression level is set at compression level 1-9 compression level the higher the compression consumption of resources, generally take 4-5 level compromise more appropriate
Gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml Application/xml+rss text/javascript;//enable compressed file type specific format parameter reference/usr/local/nginx/conf/mime.types file
......
}
5. Customize error page to improve experience
Error_page 404/404.html; Custom error page to a 404 page in the root directory of the Web site instead of the default 404 error
Location =/404.html {
root HTML;
}

Linux Redhat 7 Nginx optimized configuration

Related Article

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.