Press "Enter" to skip to content

标签: nginx

FreeBSD12安装Nginx1.18+PHP7.4+MariaDB10.4的网站环境

个人很喜欢FreeBSD系统,简单,占用资源少,在网站大并发处理方面性能比Linux要好很多,稳定运行三五年都不用重启,因为之前做广告投放系统,每天大并发300多万,Linux并发一上来压力就大系统就慢下来,搞不好就挂机了,然而FreeBSD安然无恙,所以还有什么理由不选择这么优秀的系统来做大并发服务呢?(当然这里还涉及到很多系统安全及优化操作,等空了再分享给大家,先记录一下基本环境的搭建)

常用的工具
pkg install vim wget screen
编译扩展时用到的
pkg install autoconf

这里需要的扩展就自己选择增加吧,不知道的可以用才下工具查出你想要的扩展吧

pkg search php74

开始NGINX+PHP7.4.9+MARIADB的安装:

pkg install php74 php74-zlib php74-exif php74-gd php74-mbstring php74-xml php74-pdo php74-pdo_mysql php74-pdo_pgsql php74-mysqli php74-curl php74-opcache php74-intl php74-sockets php74-zip php74-openssl php74-bz2 php74-pecl-redis php74-session php74-json php74-dom php74-fileinfo php74-simplexml php74-simplexml php74-pdo_sqlite php74-iconv php74-pecl-imagick-im7 php74-filter php74-phar php74-calendar php74-ctype php74-exif php74-bcmath php74-dba php74-tokenizer php74-extensions php73-zip nginx-full mariadb104-server-10.4.13_4 mariadb104-client-10.4.13_4

开机启动项/etc/rc.conf增加:

php_fpm_enable="yes"
nginx_enable="yes"
mysql_enable="yes"
mysql_pidfile="/var/db/mysql/mysql.pid"
mysql_optfile="/usr/local/etc/my.cnf"

PHP配置/usr/local/etc/php-fpm.d/www.conf

[www]
user = www
group = www

;listen = 127.0.0.1:9000
 (注释掉或删除,添加以下这一行/tmp/php-cgi.sock)
listen = /tmp/php-cgi.sock

开启以下三行
listen.owner = www
listen.group = www
listen.mode = 0660

为什么要用unix:/tmp/php-cgi.sock替代127.0.0.1:9000呢?最主要的就是unix socket比tcp快,所以对大并发的网站优化过后性能提升不少,这一点你GET到了吗?启动php-fpm自动生成/tmp/php-cgi.sock。

对php.ini文件的修改,增加重要的函数进行屏蔽

disable_functions = passthru,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
根据服务器内存大小调整
memory_limit = 128M
上传文件最大5M
upload_max_filesize = 5M

根据需要开启opcache缓存,能提升性能
[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=10000
opcache.max_wasted_percentage=5
opcache.validate_timestamps=1
opcache.revalidate_freq=10

validate_timestamps配置项用于验证是否要重新生成缓存脚本, 如果设置为 0(性能最佳),需要手动在每次 PHP 代码更改后手动清除 opcache。除了重启php-fpm的进程可以清理opcache缓存外, 通过PHP函数可以手动清除:

<?php opcache_reset();?>

网络上其他朋友说,当PHP以PHP-FPM的方式运行的时候,opcache的缓存是无法通过php命令进行清除的,只能通过http或cgi到php-fpm进程的方式来清除缓存(此说法本人未验证过)。我配置的生产环境一般都会通过重启php-fpm进程来清除缓存,所以一般一直缓存着高效。

validate_timestamps配置项如果值为 0,那么 revalidate_freq 将失去作用。
revalidate_freq 用于控制 opcache 多久生成一次缓存字节码,这里配置了默认 10s。所以一般我们在开发环境中将上面两个值配置为:

opcache.validate_timestamps=1;
opcache.revalidate_freq=1;

php.net上有人提供的方法存放到 /usr/local/bin/opcache-clear ,需要重置的时候执行一下: “opcache-clear”(仅供参考):

#!/bin/bash
WEBDIR=/var/www/html/
RANDOM_NAME=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 13)
echo "<?php opcache_reset(); ?>" > ${WEBDIR}${RANDOM_NAME}.php
curl http://localhost/${RANDOM_NAME}.php
rm ${WEBDIR}${RANDOM_NAME}.php

PHP开发的针对opcache进行图形化的参考:

https://github.com/rlerdorf/opcache-status

https://github.com/PeeHaa/OpCacheGUI

nginx.conf中修改

# fastcgi_pass 127.0.0.1:9000;

改为:
fastcgi_pass unix:/tmp/php-cgi.sock;

# fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
改为:
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

默认WEB目录:/usr/local/www/nginx-dist 执行PHP可能需要添加相应的执行权限及目录用户归www
保存后重启php-fpm和nginx:

root@kiccleaf:/home # service php-fpm restart
root@kiccleaf:/home # service nginx restart

启动Mysql,正常情况下

root@kiccleaf:/usr/local/etc # service mysql-server start
Installing MariaDB/MySQL system tables in '/var/db/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is mysql@localhost, it has no password either, but
you need to be the system 'mysql' user to connect.
After connecting you can set the password, if you would need to be
able to connect as any of these users with a password and without sudo

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '/usr/local' ; /usr/local/bin/mysqld_safe --datadir='/var/db/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/local/mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
Get Involved
Starting mysql.

mysql如果进程中没有看到说明启动失败,可以看错误信息文件:/var/log/mysql/mysqld.err

2020-08-29 22:52:52 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2020-08-29 22:52:52 0 [Note] InnoDB: Uses event mutexes
2020-08-29 22:52:52 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-08-29 22:52:52 0 [Note] InnoDB: Number of pools: 1
2020-08-29 22:52:52 0 [Note] InnoDB: Using SSE2 crc32 instructions
2020-08-29 22:52:52 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-08-29 22:52:52 0 [Note] InnoDB: Completed initialization of buffer pool
2020-08-29 22:52:52 0 [Note] InnoDB: 128 out of 128 rollback segments are active.
2020-08-29 22:52:52 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2020-08-29 22:52:52 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2020-08-29 22:52:52 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
2020-08-29 22:52:52 0 [Note] InnoDB: Waiting for purge to start
2020-08-29 22:52:52 0 [Note] InnoDB: 10.4.13 started; log sequence number 61026; transaction id 21
2020-08-29 22:52:52 0 [Note] InnoDB: Loading buffer pool(s) from /var/db/mysql/ib_buffer_pool
2020-08-29 22:52:52 0 [Note] InnoDB: Buffer pool(s) load completed at 200829 22:52:52
2020-08-29 22:52:52 0 [Note] Plugin 'FEEDBACK' is disabled.
2020-08-29 22:52:52 0 [Note] Server socket created on IP: '127.0.0.1'.
2020-08-29 22:52:52 0 [ERROR] Can't start server : Bind on unix socket: Permission denied
2020-08-29 22:52:52 0 [ERROR] Do you already have another mysqld server running on socket: /var/run/mysql/mysql.sock ?
2020-08-29 22:52:52 0 [ERROR] Aborting

关键的错误信息已经提示:Can’t start server : Bind on unix socket: Permission denied 和Do you already have another mysqld server running on socket: /var/run/mysql/mysql.sock ?启动权限有关,需要找到启动:/var/run/mysql目录 权限:root wheel需要修改成mysql用户权限,然后重启mysql就成功了。

原目录权限
drwxr-xr-x  2 root  wheel      512 Aug 29 22:09 mysql/

变更目录所有者权限
root@kiccleaf:/var/run # chown -R mysql:mysql mysql/

运行mysql
root@kiccleaf:/var/run # service mysql-server start
Starting mysql.
查看一下mysql目录下是否生成了mysql.sock文件
root@kiccleaf:/var/run # ll mysql/
total 0
srwxrwxrwx  1 mysql  mysql  0 Aug 29 23:03 mysql.sock=
查看进程是否已经启动,找到mysqld_safe 
root@kiccleaf:/var/run # ps aux
mysql 1092   0.0  0.0  11988  2976  -  Ss   23:03     0:00.99 /bin/sh /usr/local/bin/mysqld_safe --defaults-extra-file=/usr/local/etc/my.cnf --user=mysql

接下去初始mysql操作

root@kiccleaf:/var/run # /usr/local/bin/mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

#输入mysql的root密码。默认没有,直接回车
Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

#是否切换到unix套接字身份验证[Y/n]
Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

#是否设置root密码,选Y,输入2次需要设定的密码
Change the root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

#是否删除匿名用户?果断删除
Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

#是否不允许远程root登录,正式环境下应该选择Y,我是在测试环境需要连接。
Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

#是否删除test数据库
Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

#是否加载权限使之生效
Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

在默认的WEB目录下/usr/local/www/nginx-dist创建index.php内容:

<?php
phpinfo();
?>

打开浏览器,输入http://IP地址/index.php,可以看到成功的页面了。到此FreeBSD12系统下的基础配置已经完成。

Leave a Comment

FreeBSD升级Nginx

最新nginx版本解决了SSL漏洞,所以需要升级一下nginx版本至最新,以下是FreeBSD下用pkg升级的方法。

root@kiccleaf:~ # nginx -v
nginx version: nginx/1.12.0
root@kiccleaf:~ # pkg upgrade nginx
Updating FreeBSD repository catalogue...
Fetching meta.txz: 100%    944 B   0.9kB/s    00:01    
Fetching packagesite.txz: 100%    6 MiB  95.5kB/s    01:11    
Processing entries: 100%
FreeBSD repository update completed. 33333 packages processed.
The following 2 package(s) will be affected (of 0 checked):

Installed packages to be UPGRADED:
	nginx: 1.12.0_1,2 -> 1.14.0_13,2
	pcre: 8.40_1 -> 8.42

The process will require 178 KiB more space.
2 MiB to be downloaded.

Proceed with this action? [y/N]: y
Fetching nginx-1.14.0_13,2.txz: 100%  416 KiB 425.5kB/s    00:01    
Fetching pcre-8.42.txz: 100%    1 MiB   1.2MB/s    00:01    
Checking integrity... done (0 conflicting)
[1/2] Upgrading pcre from 8.40_1 to 8.42...
[1/2] Extracting pcre-8.42: 100%
[2/2] Upgrading nginx from 1.12.0_1,2 to 1.14.0_13,2...
You may need to manually remove /usr/local/etc/nginx/nginx.conf if it is no longer needed.
===> Creating groups.
Using existing group 'www'.
===> Creating users
Using existing user 'www'.
[2/2] Extracting nginx-1.14.0_13,2: 100%
Message from nginx-1.14.0_13,2:
===================================================================
Recent version of the NGINX introduces dynamic modules support.  In
FreeBSD ports tree this feature was enabled by default with the DSO
knob.  Several vendor's and third-party modules have been converted
to dynamic modules.  Unset the DSO knob builds an NGINX without
dynamic modules support.

To load a module at runtime, include the new `load_module'
directive in the main context, specifying the path to the shared
object file for the module, enclosed in quotation marks.  When you
reload the configuration or restart NGINX, the module is loaded in.
It is possible to specify a path relative to the source directory,
or a full path, please see
https://www.nginx.com/blog/dynamic-modules-nginx-1-9-11/ and
http://nginx.org/en/docs/ngx_core_module.html#load_module for
details.

Default path for the NGINX dynamic modules is

/usr/local/libexec/nginx.
===================================================================
root@kiccleaf:~ # nginx -v
nginx version: nginx/1.14.0
Comments closed

Nginx HTTP Post Method: 405 Method not allowed解决方法

最近维护一台RedHat 5.4 X64系统,环境是Nginx,跑着一个论坛,需要向HTML页面提交POST数据,结果都被拦截下来了,显示错误:“nginx 405 Not Allowed”,是乎没有很好的解决办法,唯一能做的就是重新编译Nginx源码和编辑conf文件。
需要修改Nginx中的C源码文件位于 /nginx源码目录/src/http/modules/ngx_http_static_module.c ,找到如下代码:

    if (r->method == NGX_HTTP_POST) {
        return NGX_HTTP_NOT_ALLOWED;
    }


注释掉如下:

/*if (r->method == NGX_HTTP_POST) {
        return NGX_HTTP_NOT_ALLOWED;
    }
*/


然后再重新编译 make
复制/nginx源码目录/ objs 目录下的 nginx至安装的Nginx目录下,重启Nginx生效。

对于Nginx,可以修改nginc.conf配置文件,改变“405错误”为“200 ok”,并配置location来解决,方法如下:

server
{
    listen       80;
    server_name  www.kiccleaf.com;
    index index.html index.htm index.php;
    root  /data/kiccleaf;  

    if ($host != 'www.kiccleaf.com' ) {
         rewrite ^/(.*)$ http://www.kiccleaf.com/$1 permanent;
    }
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    #添加以下405代码
    error_page   405 =200 @405;
    location @405
    {
        root  /data/kiccleaf;
    }
}


也可以简单的编写成

server
{
    listen       80;
    server_name  www.kiccleaf.com;
    index index.html index.htm index.php;
    root  /data/kiccleaf;  

    if ($host != 'www.kiccleaf.com' ) {
         rewrite ^/(.*)$ http://www.kiccleaf.com/$1 permanent;
    }
    location ~ .*\.(php|php5)?$
    {
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }
    #添加以下405代码
    error_page 405 =200 $uri;
}
Leave a Comment

Nginx 301重定向配置方法

有时候顶级域名不想使用,只想用www域名,那么需要做301重定向,在Nginx下的方法有以下两种我较常用的方法:

第一种方式建立一个kiccleaf.com.conf文件输入以下内容

server {
    listen 80;
    server_name  kiccleaf.com;
    location / {
            rewrite ^(.*) http://www.kiccleaf.com$1 permanent;
    }
}

第二种方式在原有的www.kiccleaf.com.conf文件里添加至server_name下一行(我个人一般采用这种方式,比较方便在一个配置文件中就可以解决了。)

server{
   listen 80;
   server_name www.kiccleaf.com kiccleaf.com ;
   if ($host != 'www.kiccleaf.com' ){
      rewrite ^/(.*)$ http://www.kiccleaf.com/$1 permanent;
   }
  ...
}
Leave a Comment

配置Nginx多核CPU,worker_cpu_affinity方法

由于现今服务器的CPU基本是四核或是八核以上,Nginx默认是没有开启利用多核CPU,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核CPU。实现高性能处理数据和利用CPU资源

1. 2核CPU,开启2个进程

worker_processes     2;
worker_cpu_affinity 01 10;

01表示启用第一个CPU内核,10表示启用第二个CPU内核
worker_cpu_affinity 01 10;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。
2. 2核CPU,开启4个进程

worker_processes     4;
worker_cpu_affinity 01 10 01 10;

开启了四个进程,它们分别对应着开启2个CPU内核

3. 4核CPU,开户4个进程

worker_processes     4;
worker_cpu_affinity 0001 0010 0100 1000;

0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推

4. 4核CPU,开启2个进程

worker_processes     2;
worker_cpu_affinity 0101 1010;

0101表示开启第一个和第三个内核,1010表示开启第二个和第四个内核

2个进程对应着四个内核

worker_cpu_affinity配置是写在/etc/nginx/nginx.conf里面的。

2核是 01,四核是0001,8核是00000001,有多少个核,就有几位数,1表示该内核开启,0表示该内核关闭。

5. 8核CPU,开户8个进程

worker_processes     8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推

worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。

配置完毕后,重启nginx ,执行/etc/init.d/nginx restart 或是安装目录下:/nginx/sbin/nginx -s reload 重新加载

输入:top 命令后按 1 进行查看

Leave a Comment

Linux X64下Nginx1.0.4无缝升级到新版本1.3.0版本操作方法[原创]

服务器近一年没有更新Nginx版本了,为了提高性能和稳定,采用了最新的Nginx版本。可以到http://nginx.org/en/download.html 进行下载。升级仅花了几分钟时间就搞定了。
注:以下代码是真实环境直接配置复制进来,可以根据代码从上至下进行操作配置。

[root@kiccleaf leaf]# wget http://nginx.org/download/nginx-1.3.0.tar.gz
[root@kiccleaf leaf]# tar -zxvf nginx-1.3.0.tar.gz
[root@kiccleaf leaf]# cd nginx-1.3.0
#如果需要服务器个性化名称可以修改
[root@kiccleaf nginx-1.3.0]# vi src/core/nginx.h
/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_


#define nginx_version      1003000
#define NGINX_VERSION      "1.3.0"  #可修改成自己需要的版本
#define NGINX_VER          "Nginx/" NGINX_VERSION #可修改成自己需要服务的名称

#define NGINX_VAR          "NGINX"  #可修改成自己需要服务的名称
#define NGX_OLDPID_EXT     ".oldbin"


#endif /* _NGINX_H_INCLUDED_ */

#进行配置操作,根据自己的实际配置来,以下是我简单的配置

[root@kiccleaf nginx-1.3.0]# ./configure --user=www --group=www --add-module=../ngx_cache_purge-1.3 --prefix=/usr/local/nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
checking for OS
 + Linux 2.6.18-274.12.1.el5 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.1.2 20080704 (Red Hat 4.1.2-51)
…………………………编译代码省略………………
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"
[root@kiccleaf nginx-1.3.0]# make  #看清咯,只make,而不是make install,这里升级不需要install操作
make -f objs/Makefile
make[1]: Entering directory `/home/leaf/nginx-1.3.0'
…………………………编译代码省略………………
make[1]: Leaving directory `/home/leaf/nginx-1.3.0'
make -f objs/Makefile manpage
make[1]: Entering directory `/home/leaf/nginx-1.3.0'
sed -e "s|%%PREFIX%%|/usr/local/nginx|" \
		-e "s|%%PID_PATH%%|/usr/local/nginx/logs/nginx.pid|" \
		-e "s|%%CONF_PATH%%|/usr/local/nginx/conf/nginx.conf|" \
		-e "s|%%ERROR_LOG_PATH%%|/usr/local/nginx/logs/error.log|" \
		< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/home/leaf/nginx-1.3.0'

检查配置是否正确,这里出现了警告,因为从低版本升级至高版本产生了配置的问题

[root@kiccleaf nginx-1.3.0]# ./objs/nginx -t
nginx: [warn] the "log_format" directive may be used only on "http" level in /usr/local/nginx/conf/conf.d/www.kiccleaf.com.conf:22
nginx: [warn] the "log_format" directive may be used only on "http" level in /usr/local/nginx/conf/conf.d/www.qimutian.com.conf:22
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#升级后出现配置文件conf有错误,如果有其的虚拟主机开启了日志,也按要求移出server段放在server段的前面即可。
[root@kiccleaf nginx-1.3.0]# vi /usr/local/nginx/conf/conf.d/www.kiccleaf.com.conf
#把server里面的log_format代码拿到上面来,保存退出
    log_format  www_kiccleaf_com  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
server
{
    listen       80;
    server_name  www.kiccleaf.com;
    index index.html index.htm index.php;
    root  /data/kiccleaf;
    #limit_conn   crawler  20;    
                            
    location ~ .*\.(php|php5)?$
    {      
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }

#以下代码的问题,升级后需要把以下代码拿到server上面去。这里先注释掉
#    log_format  www_kiccleaf_com  '$remote_addr - $remote_user [$time_local] "$request" '
#             '$status $body_bytes_sent "$http_referer" '
#              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /data/logs/www_kiccleaf_com.log www_kiccleaf_com;

}
[root@kiccleaf nginx-1.3.0]# /usr/local/nginx/sbin/nginx -t  #再检查,没有warn警告信息了
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

操作升级,出错误不管它

[root@kiccleaf nginx-1.3.0]# make upgrade #出现错误,就先不管它
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
cat: /usr/local/nginx/logs/nginx.pid: No such file or directory
kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
make: *** [upgrade] Error 1

备份老的Nginx程序,复制新的过去

[root@kiccleaf nginx-1.3.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
mv: overwrite `/usr/local/nginx/sbin/nginx.old'? y
[root@kiccleaf nginx-1.3.0]# cp objs/nginx /usr/local/nginx/sbin/nginx
[root@kiccleaf nginx-1.3.0]# /usr/local/nginx/sbin/nginx -t #查看复制过去配置是否正确
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@kiccleaf nginx-1.3.0]# /usr/local/nginx/sbin/nginx -v  #再查看一下新版本,老的是1.0.4版本
nginx version: Nginx/1.3.0  #已经是新版本

无缝升级到新版本

[root@kiccleaf nginx-1.3.0]# kill -USR2 `cat /usr/local/nginx/nginx.pid` #
[root@kiccleaf nginx-1.3.0]# ps aux| grep nginx #查看一下进程
root      2169  0.0  0.0 248708  1788 ?        Ss    2011   0:00 nginx: master process /usr/local/nginx/sbin/nginx  #老的进程都是2011年开启的,嘿嘿
www       6891  0.0  0.3 274308 27796 ?        S    Apr20   0:13 nginx: worker process               
www       6892  0.0  0.3 274308 27616 ?        S    Apr20   0:13 nginx: worker process               
www       6893  0.0  0.3 274308 27596 ?        S    Apr20   0:13 nginx: worker process               
www       6894  0.0  0.3 274308 27612 ?        S    Apr20   0:13 nginx: worker process               
www       6895  0.0  0.3 274308 27608 ?        S    Apr20   0:15 nginx: worker process               
www       6896  0.0  0.3 274308 27796 ?        S    Apr20   0:15 nginx: worker process               
www       6897  0.0  0.3 274308 27700 ?        S    Apr20   0:15 nginx: worker process               
www       6898  0.0  0.3 274308 27600 ?        S    Apr20   0:12 nginx: worker process               
www       6899  0.0  0.3 274308 27592 ?        S    Apr20   0:13 nginx: worker process               
www       6900  0.0  0.3 274308 27620 ?        S    Apr20   0:13 nginx: worker process               
www       6901  0.0  0.3 274308 27616 ?        S    Apr20   0:14 nginx: worker process               
www       6902  0.0  0.3 274308 27652 ?        S    Apr20   0:11 nginx: worker process               
www       6903  0.0  0.3 274308 27856 ?        S    Apr20   0:13 nginx: worker process               
www       6904  0.0  0.3 274308 27608 ?        S    Apr20   0:15 nginx: worker process               
www       6905  0.0  0.3 274308 27644 ?        S    Apr20   0:12 nginx: worker process               
www       6906  0.0  0.3 274308 27612 ?        S    Apr20   0:16 nginx: worker process               
www       6907  0.0  0.0 248708  1500 ?        S    Apr20   0:00 nginx: cache manager process        
root     30293  0.0  0.0 248008  3764 ?        S    22:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
www      30294  0.0  0.3 273968 27524 ?        S    22:17   0:00 nginx: worker process               
www      30295  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30296  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30297  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30298  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30299  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30300  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30301  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30302  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30303  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30304  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30305  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30306  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30307  0.0  0.3 273936 27392 ?        S    22:17   0:00 nginx: worker process               
www      30308  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30309  0.0  0.3 273608 26784 ?        S    22:17   0:00 nginx: worker process               
www      30310  0.0  0.0 248188  1396 ?        S    22:17   0:00 nginx: cache manager process        
root     30316  0.0  0.0  61172   800 pts/1    S+   22:36   0:00 grep nginx

原来存放nginx.pid的目录下自动出现了nginx.pid.oldbin。现在新的和旧的一起在提供服务,我们执行一下命令把老的进程给Kill掉就可以了。

[root@kiccleaf nginx-1.3.0]# kill -QUIT `cat /usr/local/nginx/nginx.pid.oldbin` 
[root@kiccleaf nginx-1.3.0]# ps aux| grep nginx #现在查看进程只有新的了
root     30293  0.0  0.0 248720  3924 ?        S    22:17   0:00 nginx: master process /usr/local/nginx/sbin/nginx
www      30332  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30333  0.1  0.3 274320 27552 ?        S    22:43   0:00 nginx: worker process               
www      30334  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30335  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30336  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30337  0.1  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30338  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30339  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30340  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30341  0.1  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30342  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30343  0.1  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30344  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30345  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30346  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30347  0.0  0.3 274320 26944 ?        S    22:43   0:00 nginx: worker process               
www      30348  0.0  0.0 248720  1400 ?        S    22:43   0:00 nginx: cache manager process        
root     30418  0.0  0.0  61172   796 pts/1    S+   22:45   0:00 grep nginx

好了进行访问网站,是不是正常不间断切换至最新的Nginx版本了!

Leave a Comment

Nginx目录自动加斜线”/”

服务器配置成Nginx后,发现子目录结尾不加“/”会造成无法访问。这是大多数人从Apache转至nginx的问题。

如:http://www.kiccleaf.com/test 就无法正常访问,而http://www.kiccleaf.com/test/ 却可以正常访问。

解决方法在nginx.conf中增加:
找到http{ 然后在里面添加

http{
…
server_name_in_redirect off;
…
}
Leave a Comment