nginx
nginx 查看配置文件
首先执行命令找到nginx路径
ps aux | grep nginx
如nginx路径为
/usr/local/nginx/sbin/nginx
nginx -t #可以查到配置文件的信息
---------------------------------------------------------------------------------
nginx 安装
wget http://mirrors.sohu.com/php/php-5.6.2.tar.gz yum -y install gcc gcc-c++ libxml2 libxml2-devel tar -xf php-5.6.2.tar.gz cd php-5.6.2 find / -name mysql /var/lib/mysql ll /var/lib/mysql ./configure -enable-fpm -enable-mbstring -with-mysql=/usr/bin/mysql find / -name mysql.h yum list mysql-devel ./configure -enable-fpm -enable-mbstring -with-mysql=/usr;
#这个配置比较全
#./configure --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --disable-fileinfo --enable-opcache --enable-intl --with-xsl
make;
#make ZEND_EXTRA_LIBS='-liconv' #collect2: error: ld returned 1 exit status
make install
php -v
cp php.ini-production /usr/local/lib/php.ini
cp /usr/local/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
/etc/init.d/php-fpm start
yum -y install pcre* yum -y install openssl* wget mirrors.sohu.com/nginx/nginx-1.9.0.tar.gz tar -zxvf nginx-1.9.0.tar.gz cd nginx-1.9.0 ./configure --prefix=/usr/local/nginx --with-http_ssl_module make make install find / -name nginx; #/usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx; #启动 nginx 进程 find / -name nginx.conf; vi /usr/local/nginx/conf/nginx.conf; #进行配置 killall nginx; #杀掉进程 /usr/local/nginx/sbin/nginx -s start; #启动
/usr/local/nginx/sbin/nginx -s reload; #平滑启动配置 vi /etc/rc.local; #配置开机启动
---------------------------------------------------------------------------------
nginx 简单操作
killall nginx - 直接关闭进程
/usr/local/nginx/sbin/nginx - 这样开启进程
/usr/local/nginx/sbin/nginx -s reload - 重新加载配置
/usr/local/nginx/sbin/nginx -s stop - 停止
---------------------------------------------------------------------------------
nginx 开机启动
在 /etc/rc.local 文件,增加一行 /usr/local/nginx/sbin/nginx
---------------------------------------------------------------------------------
首次配置文
http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; include /usr/local/nginx/host/*.conf; #可以把配置文件分成多个,不必全部网站的配置都写在一起。 # another virtual host using mix of IP-, name-, and port-based configuration #
server { listen 80; charset GBK; server_name localhost 192.168.2.31; root /var/www/html/localhost; access_log logs/$fastcgi_script_name.log; #location / { index index.php;} location ~ .*\.(php|PHP)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/localhost$fastcgi_script_name; #我擅作主张把$fastcgi_script_name,导致提示access denied错误 include fastcgi_params; fastcgi_intercept_errors on; } }
****************************************** 2016-07-26 ******************************************
2016/07/26 11:30:07 [crit] 16632#0: *1070 open() "/usr/local/nginx/logs//****/makehtml_archives_action.php.log" failed (2: No such file or directory) while logging request, client: 19.155.66.50, server: localhost, request: "GET /****/makehtml_archives_action.php?endid=0&startid=0&typeid=0&totalnum=3077&startdd=2640&pagesize=20&seltime=0&sstime=1469503757&stime=&etime=&uptype=mkall&mkvalue=0&isremote=0&serviterm= HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "**********", referrer: "http://*********/***********/makehtml_archives_action.php?endid=0&startid=0&typeid=0&totalnum=3077&startdd=2620&pagesize=20&seltime=0&sstime=1469503757&stime=&etime=&uptype=mkall&mkvalue=0&isremote=0&serviterm="
nginx 的 error_log 日志经常提示这个。应该是配置文件的 access.log 没配置好。配置文件中的 $fastcgi_script_name 应该指的是运行文件的名称。修改为:
server { listen 6080; charset UTF8; server_name localhost; root /var/www/html/mmgsj; access_log logs/access.log-mmgjs; #location / { index index.php;} location ~ .*\.(php|PHP)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/mmgsj$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; } }
rewrite 例子
rewrite ^(.*)-htm-(.*)$ $1.php?$2 last; rewrite ^(.*)/simple/([a-z0-9\_]+\.html)$ $1/simple/index.php?$2 last; rewrite ^(.*)/data/(.*)\.(htm|php)$ 404.html last; rewrite ^(.*)/attachment/(.*)\.(htm|php)$ 404.html last; rewrite ^(.*)/html/(.*)\.(htm|php)$ 404.html last;
ThinkPHP 配置的例子
server { listen 80; charset UTF8; server_name test.com; root /var/www/html/test; access_log logs/access.log-test; #location ~ .*\.(php|PHP)?$ { # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /var/www/html/test_soso$fastcgi_script_name; # include fastcgi_params; # fastcgi_intercept_errors on; #} #去掉$是为了不匹配行末,即可以匹配.php/,以实现pathinfo #如果你不需要用到php5后缀,也可以将其去掉 location ~ .php { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/test_soso$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; #定义变量 $path_info ,用于存放pathinfo信息 set $path_info ""; #定义变量 $real_script_name,用于存放真实地址 set $real_script_name $fastcgi_script_name;
#如果地址与引号内的正则表达式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#将文件地址赋值给变量 $real_script_name
set $real_script_name $1;
#将文件地址后的参数赋值给变量 $path_info
set $path_info $2;
}
#配置fastcgi的一些参数
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
}
配置 Laravel 的例子
server { listen 80; charset UTF8; server_name www.mylaravel.com; set $root_path '/var/www/html/laravel'; root $root_path; access_log logs/access.laravel.log; error_log logs/error.laravel.log; location / { rewrite ^(.*)$ /public/index.php last; } location ~ .*\.(js|css|svg|jpg|ttf|woff2|woff|png)$ { root $root_path/public/; } location ~ .*\.(php|PHP)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $root_path$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; } }
location /qyapi/ { rewrite ^(.*)$ /qyapi/public/index.php last; } location ~ /qyapi/.*\.(html|htm)$ { root $root_path/; } location ~ /qyapi/.*\.(js|css|svg|jpg|ttf|woff2|woff|png)$ { root $root_path/; }
下面是php 5.3以上版本将TCP改成socket方式的配置方法: 修改php-fpm.conf(/usr/local/php/etc/php-fpm.conf) ;listen = 127.0.0.1:9000 listen = /dev/shm/php-cgi.sock 修改nginx配置文件server段的配置,将http的方式改为socket方式 location ~ .*\.(php|php5)?$ { #fastcgi_pass 127.0.0.1:9000; fastcgi_pass unix:/dev/shm/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; } 重启php-fpm与nginx service nginx restart service php-fpm restart ls -al /dev/shm 可以看到php-cgi.sock文件unix套接字类型 理论上,unix socket 不走网络,效率高一些,但稳定性不是很理想,看这个测试:
alias location /abc/ { alias /home/html/abc/; } #在这段配置下,http://test/abc/a.html就指定的是 /home/html/abc/a.html。这段配置亦可改成 root location /abc/ { root /home/html/;} #可以看到,使用root设置目录的绝对路径时,少了/abc,也就是说,使用root来设置前端非根目录时,nginx会组合root和location的路径。 #另外,使用alias时目录名后面一定要加“/”
问题与解决
1. php-fpm.log日志文件,提示错误
server reached pm.max_children setting (5), consider raising it
把 pm.max_children 设置为 500
后来又提示
you may need to increase pm.start_servers, or pm.min/max_spare_servers
location 的配置
location 优先级官方文档 Directives with the = prefix that match the query exactly. If found, searching stops. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops. Regular expressions, in order of definition in the configuration file. If #3 yielded a match, that result is used. Else the match from #2 is used. 1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。 2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^〜前缀,搜索停止。 3. 正则表达式,在配置文件中定义的顺序。 4. 如果第3条规则产生匹配的话,结果被使用。否则,使用第2条规则的结果。
http://www.nginx.cn/115.html
https://linux.cn/article-5714-1.html
配置 nginx.service
# /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target