/ 中存储网

Linux系统中Nginx服务器的配置示例

2013-11-21 14:27:02 来源:ITJS.CN

1,Nginx主配置文件及FasgCGI配置文件

nginx.conf

========================================================================

user www www;

worker_processes      8;

worker_rlimit_nofile 51200;

error_log /root/workspace/logs/nginx/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

pid        logs/nginx.pid;

events {

use epoll;

worker_connections 51200;

}

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;

tcp_nodelay     on;

#keepalive_timeout 0;

keepalive_timeout 65;

gzip on;

gzip_types         text/plain text/css application/x-javascript application/xml;

fastcgi_connect_timeout   500;

fastcgi_read_timeout      300;

fastcgi_send_timeout      300;

fastcgi_intercept_errors   on;

server {

listen       80;

server_name www.workspace.com;

index        index.html index.php;

root         /root/workspace/htdocs;

access_log   /root/workspace/logs/nginx/host.access.log main;

location ~ .php$ {

fastcgi_pass   127.0.0.1:9000;

fastcgi_index index.php;

#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include        fastcgi_params;

}

location ~ .(gif|jpg|jpeg|png|swf|bmp)$ {

expires        30d;

}

location ~ .(css|js)$ {

expires        1h;

}

#charset koi8-r;

#error_page 404              /404.html;

# redirect server error pages to the static page /50x.html

#

#error_page   500 502 503 504 /50x.html;

#location = /50x.html {

#    root   html;

#}

# proxy the PHP script to Apache listening on 127.0.0.1:80

#

#localtion ~ .php$ {

#    proxy_pass http://127.0.0.1;

#}

# deny access to .htaccess files, if Apache's document root

# concurs with nginx's one

#

#location ~ /.ht {

#    deny all;

#}

}

server {

listen        80;

server_name   www.dbspace.com;

index         index.html index.php;

root          /root/workspace/mysql;

location ~ .php$ {

fastcgi_pass   127.0.0.1:9000;

fastcgi_index index.php;

include        fastcgi_params;

}

location ~ .(gif|jpg|jpeg|png|swf|bmp)$ {

expires 30d;

}

location ~ .(css|js)$ {

expires 1h;

}

}

#another virtual host using mix of IP-, name-, and port-based configuration

#

#server {

#    listen       8000;

#    listen       somename:8080;

#    server_name somename alias another.alias;

#    location / {

#        root   html;

#        index index.html index.htm;

#    }

#}

# HTTPS server

#

#server {

#    listen       443;

#    server_name localhost;

#    ssl                  on;

#    ssl_certificate      cert.pem;

#    ssl_certificate_key cert.key;

#    ssl_session_timeout 5m;

#    ssl_protocols SSLv2 SSLv3 TLSv1;

#    ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

#    ssl_prefer_server_ciphers   on;

#    location / {

#        root   html;

#        index index.html index.htm;

#    }

#}

}

fastcgi_params

========================================================================

fastcgi_param QUERY_STRING       $query_string;

fastcgi_param REQUEST_METHOD     $request_method;

fastcgi_param CONTENT_TYPE       $content_type;

fastcgi_param CONTENT_LENGTH     $content_length;

fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;

fastcgi_param SCRIPT_NAME        $fastcgi_script_name;

fastcgi_param REQUEST_URI        $request_uri;

fastcgi_param DOCUMENT_URI       $document_uri;

fastcgi_param DOCUMENT_ROOT      $document_root;

fastcgi_param SERVER_PROTOCOL    $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;

fastcgi_param SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param REMOTE_ADDR        $remote_addr;

fastcgi_param REMOTE_PORT        $remote_port;

fastcgi_param SERVER_ADDR        $server_addr;

fastcgi_param SERVER_PORT        $server_port;

fastcgi_param SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect

fastcgi_param REDIRECT_STATUS    200;

2,Nginx + PHP + MySQL简易管理Shell脚本

nginx

========================================================================

#!/bin/bash

function_start_nginx(){

printf "Starting Nginx ...n"

ulimit -SHn 51200

/usr/local/nginx/sbin/nginx 2>&1 > /dev/null &

}

function_stop_nginx(){

printf "Stoping Nginx ...n"

ps -ef | grep "nginx:" | grep -v grep | awk '{print "kill -9 " $2}' | /bin/sh

}

function_reload_nginx(){

printf "Reloading Nginx ...n"

ps -ef | grep "nginx: master proccess" | grep -v grep | awk '{print "kill -HUP " $2}' | /bin/sh

}

function_restart_nginx(){

printf "Restarting Nginx ...n"

function_stop_nginx

sleep 3

function_start_nginx

}

if [ "$1" = "start" ]; then

function_start_nginx

elif [ "$1" = "stop" ]; then

function_stop_nginx

elif [ "$1" = "reload" ]; then

function_reload_nginx

elif [ "$1" = "restart" ]; then

function_restart_nginx

else

printf "Usage: nginx {start|stop|reload|restart} n"

fi

php

========================================================================

#!/bin/bash

function_start_php(){

printf "Starting PHP ...n"

ulimit -SHn 51200

/usr/local/php/sbin/php-fpm start 2>&1 > /dev/null &

}

function_stop_php(){

printf "Stoping PHP ...n"

/usr/local/php/sbin/php-fpm stop 2>&1 > /dev/null

}

function_reload_php(){

printf "Reloading PHP ...n"

ulimit -SHn 51200

/usr/local/php/sbin/php-fpm reload 2>&1 > /dev/null

}

function_restart_php(){

function_stop_php

sleep 3

function_start_php

}

function_quit_php(){

printf "Exiting PHP ...n"

/usr/local/php/sbin/php-fpm quit 2>&1 > /dev/null

}

if [ "$1" = "start" ]; then

function_start_php

elif [ "$1" = "stop" ]; then

function_stop_php

elif [ "$1" = "restart" ]; then

function_restart_php

elif [ "$1" = "reload" ]; then

function_reload_php

elif [ "$1" = "quit" ]; then

function_quit_php

else

printf "Usage: php {start|stop|reload|restart|quit} n"

fi

mysql

========================================================================

#!/bin/bash

mysql_port=3306

mysql_username="test"

mysql_password=""

mysql_conf="/etc/my.cnf"

mysql_sock="/var/lib/mysql/mysql.sock"

function_start_mysql(){

printf "Starting MySQL ...n"

/bin/sh /usr/bin/mysqld_safe --defaults-file=${mysql_conf} 2>&1 > /dev/null &

}

function_stop_mysql(){

printf "Stopping MySQL ...n"

/usr/bin/mysqladmin --port=${mysql_port} --user=${mysql_username} --password=${mysql_password} --socket=${mysql_sock} shutdown

}

function_restart_mysql(){

printf "Restarting MySQL ...n"

function_stop_mysql

sleep 3

function_start_mysql

}

function_kill_mysql(){

ps -ef | grep "mysqld_safe" | grep -v grep | awk '{print "kill -9 " $2}' | /bin/sh

ps -ef | grep "mysqld" | grep -v grep | awk '{print "kill -9 " $2}' | /bin/sh

}

if [ "$1" = "start" ]; then

function_start_mysql

elif [ "$1" = "stop" ]; then

function_stop_mysql

elif [ "$1" = "restart" ]; then

function_restart_mysql

elif [ "$1" = "kill" ]; then

function_kill_mysql

else

printf "Usage: mysql {start|stop|restart|kill}n"

fi

最后来个三合一:

automate

========================================================================

#!/bin/bash

file_limit=51200

mysql_port=3306

mysql_username="test"

mysql_password=""

mysql_conf="/etc/my.cnf"

mysql_sock="/var/lib/mysql/mysql.sock"

function_start(){

ulimit ${file_limit}

printf "Starting Web Service ...n"

printf "Starting MySQL ...n"

/usr/bin/mysqld_safe --defaults-file=${mysql_conf} 2>&1 > /dev/null &

printf "Starting PHP ...n"

/usr/local/php/sbin/php-fpm start 2>&1 > /dev/null &

printf "Starting Nginx ...n"

/usr/local/nginx/sbin/nginx 2>&1 > /dev/null &

printf "Done ...n"

}

function_stop(){

printf "Stoping Web Service ...n"

printf "Stoping MySQL ...n"

/usr/bin/mysqladmin --port=${mysql_port} --user=${mysql_username} --password=${mysql_password} --socket=${mysql_sock} shutdown

printf "Stoping PHP ...n"

/usr/local/php/sbin/php-fpm stop 2>&1 > /dev/null

printf "Stoping Nginx ...n"

ps -ef | grep "nginx:" | grep -v grep | awk '{print "kill -9 " $2}' | /bin/sh

printf "Done ...n"

}

if [ "$1" = "start" ]; then

function_start

elif [ "$1" = "stop" ]; then

function_stop

elif [ "$1" = "restart" ]; then

function_restart

else

printf "Usage: automate {start|stop|restart} n"

fi

分别保存并赋予可执行权限(chmod +x script_name),自己玩吧