Nginx and php configurations of linux LNMP (nginx part) and lnmpnginx
Nginx and php configurations of LNMP (nginx part)
1. nginx configuration file/usr/local/nginx/conf/nginx. conf
Clear nginx. conf and copy the following Configuration:
User nobody; worker_processes 2; error_log/usr/local/nginx/logs/nginx_error.log crit; pid/usr/local/nginx/logs/nginx. pid; worker_rlimit_nofile 51200; events {use epoll; worker_connections 6000;} http {include mime. types; default_type application/octet-stream; server_names_hash_bucket_size 3526; server_names_hash_max_size 4096; log_format combined_realip 'remote _ addr $ http_x_forwarded_fo R [$ time_local] ''$ host" $ request_uri "$ status'' "$ http_referer" "$ http_user_agent" '; sendfile on; tcp_nopush on; keepalive_timeout 30; client_header_timeout 3 m; client_body_timeout 3 m; send_timeout 3 m; connection_pool_size 256; Limit 1 k; limit 8 4 k; request_pool_size 4 k; output_buffers 4 32 k; postpone_output 1460; limit 10 m; Limit Fer_size 256 k; province/usr/local/nginx/province; proxy_temp_path/usr/local/nginx/proxy_temp; fastcgi_temp_path/usr/local/nginx/fastcgi_temp; region on; tcp_nodelay on; gzip on; gzip_min_length 1 k; gzip_buffers 4 8 k; gzip_comp_level 5; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css text/htm applicationxml; # configure Virtual Machine server {list En 80; server_name localhost; index index.html index.htm index. php; root/usr/local/nginx/html; location ~ \. Php $ {include fastcgi_params; fastcgi_pass 127.0.0.1: 9000; fastcgi_index index. php; fastcgi_param SCRIPT_FILENAME/usr/local/nginx/html $ fastcgi_script_name ;}}}
The virtual machine configuration server can be configured separately in conf/vhosts/under an order, and then added to nginx. conf:
Include/vhosts/*. conf
Verify and reload nginx:
/usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
In this case, access in the browser:
2. migrate the previously constructed discuz from lamp to nginx for running:
cd /usr/local/nginx/conf/vhosts cp vhosts.conf discuz.conf vi discuz.conf /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload
Now you can access the original discuz:
Iii. configuration of php-fpm
Configuration File Path:/usr/local/php-fpm/etc/php-fpm.conf, clear and paste the following code:
[Global] pid =/usr/local/php-fpm/var/run/php-fpm.pid error_log =/usr/local/php-fpm/var/log/php-fpm.log [www] // A site corresponds to a pool, the listening port is also different from listen = 127.0.0.1: 9000; listen =/tmp/php-fcgi.sock user = php-fpm group = php-fpm listen. owner = nobody listen. group = nobody pm = dynamic // number of dynamic processes. If static is specified, pm exists. max_children specifies the number of fixed sub-processes pm. max_children = 50 // maximum number of child processes pm. start_servers = 20 // Number of child processes at startup pm. min_spare_servers = 5 // ensure the minimum number of idle processes. If the number of idle processes is lower than this value, a new sub-process pm is added. max_spare_servers = 35 // ensure the maximum number of idle processes. If the number of idle processes exceeds this value, clean the pm. max_requests = 500 rlimit_files = 1024 slowlog =/usr/local/php-fpm/var/log/slow. log // defines the slow query log Path request_slowlog_timeout = 1 // defines the timeout value php_admin_value [open_basedir] =/data/www:/tmp // defines opne_basedir [www1] listen = 127.0.0.1: 9001; listen =/tmp/php-fcgi.sock user = php-fpm group = php-fpm listen. owner = nobody listen. group = nobody pm = dynamic pm. max_children = 50 pm. start_servers = 20 pm. min _spare_servers = 5. max_spare_servers = 35 pm. max_requests = 500 rlimit_files = 1024
Iv. nginx Advanced Configuration
1. Configure the second VM
You can add nginx. conf directly.
Include vhosts/*. conf;
In this way, you can directly create other VM configuration files under the/conf/vhosts directory.
2. Set the default Virtual Host
Modify this line in the VM configuration file. Enter an ip address in the browser to access the default VM;
Listen 80 default_server;
3. User Authentication
To use htpasswd, you must first install apache and install it directly using yum install httpd. You can use htpasswd:
/usr/bin/htpasswd -c /usr/local/nginx/conf/.htpasswd weix
Add the following in the nginx configuration file:
location / { root /data/www; auth_basic "Auth"; auth_basic_user_file /usr/local/nginx/conf/.htpasswd; }
4. Domain Name Redirection
Add the following in the nginx configuration file:
if ($host != 'www.1.com'){ rewrite ^/(.*)$ http://www.1.com/$1 permanent; }
5. Log Cutting
Configuration log:
Add:
Access_log/home/logs/discuz. log combined_realip;
Combined_realip is the log format defined in nginx. conf;
Compile shell:
Vim/usr/local/sbin/logrotate. sh
#!/bin/bash d=`date -d "-1 day" +%Y%m%d` /bin/mv /home/logs/discuz.log /home/logs/discuz_$d.log /etc/init.d/nginx - reload >/dev/null 2>/dev/null cd /home/logs gzip discuz_$d.log
By adding scripts to scheduled tasks, you can split logs and compress them every day;
6. static files do not record logs and configure Cache
Edit the corresponding virtual machine configuration file and add the following code:
loation ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; access_log off; } location ~ .*\.(js|css)?$ { expires 12h; access_log off; }
7. Anti-leech
Edit the VM configuration file and add the following code. Note that the location will only match once. If gif or jpg files are defined separately and no logs are recorded, anti-leech protection will invalidate the anti-leech protection, therefore, the configuration should be written together with other operations on these files. For example, two more lines are added to the log and cache time definitions;
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls|)$ { valid_referers none blocked server_names *.1.com *.a.com *.b.com *.baidu.com *.soso.com *.google.com *.google.cn ; if ($invalid_referer){ return 403; #rewrite ^/ http:/www.example.com/nophoto.gif; } }