ubuntu14.04安装与配置nginx服务器

去年曾经配置过nginx服务器,可惜的是,几个月前因故障磁盘被格式化。今天又要用到nginx服务,所以从新配置了一番,但这次就不是那么顺利了。在此,愿与大家分享一下经验。只是简单的局域网应用,并未复杂配置。

命令行安装:

$sudo apt-get install nginx

如果缺少依赖包,可以$sudo apt-get update之后再执行上面的命令。

 

以下是排错:

$ sudo service nginx restart
* Restarting nginx nginx [fail]
$ sudo update-rc.d nginx defaults //添加自启动
System start/stop links for /etc/init.d/nginx already exist.

$ sudo nginx -t //排错开始
nginx: [emerg] "keepalive_timeout" directive is not allowed here in /etc/nginx/nginx.conf:4
nginx: configuration file /etc/nginx/nginx.conf test failed
$ sudo service nginx restart
* Restarting nginx nginx [fail]
$ sudo nginx -t
nginx: [emerg] invalid parameter "172.16.69.86" in /etc/nginx/sites-enabled/default:21
nginx: configuration file /etc/nginx/nginx.conf test failed
$ sudo service nginx restart
* Restarting nginx nginx [fail]
$ sudo nginx -t
nginx: [emerg] invalid parameter "172.16.69.86" in /etc/nginx/sites-enabled/default:21
nginx: [emerg] unexpected end of file, expecting "}" in /etc/nginx/nginx.conf:111
nginx: configuration file /etc/nginx/nginx.conf test failed
$ sudo service nginx restart
* Restarting nginx nginx [fail]
~$ sudo nginx -t
nginx: [emerg] duplicate location "/" in /etc/nginx/sites-enabled/default:47
nginx: configuration file /etc/nginx/nginx.conf test failed
:~$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
:~$ sudo service nginx restart
* Restarting nginx nginx [ OK ]
需要注意的是:需要配置 /etc/nginx/nginx.conf和 /etc/nginx/sites-enabled/default

nginx。conf

#user www-data;
user root;
worker_processes 4;

pid /run/nginx.pid;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##


sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# Logging Settings
##

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

##
# Gzip Settings
##

gzip on;
gzip_disable "msie6";

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##

#include /etc/nginx/naxsi_core.rules;

##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##

#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;


server
{
listen 80;
server_name 172.16.69.86;

location /
{
root /;
autoindex on;
autoindex_exact_size on;
autoindex_localtime on;

}
}
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

 

 /etc/nginx/nginx.conf

user root;

worker_processes 8;

error_log /home/peking/nginxWebServer/nginx_error.log crit;

pid /usr/local/webserver/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by thisprocess.
worker_rlimit_nofile 65535;

events
{
use epoll;
worker_connections 65535;
}

http
{
include mime.types;
default_type application/octet-stream;

charset utf-8;

server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;

sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;

client_body_buffer_size 512k;
proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;


#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path /var/tmp/nginx/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为5GB。
proxy_cache_path /var/tmp/nginx/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=5g;


server #此处为缓存服务器
{
listen 8081;
server_name 172.16.69.132;

location /
{
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://172.16.69.132:80; #此处跳转到真实的图片服务器


log_format cache '***$time_local '

'$upstream_cache_status '

'Cache-Control: $upstream_http_cache_control '

'Expires: $upstream_http_expires '

'"$request" ($status) '

'"$http_user_agent" '; #定义日志格式(此日志格式可以显示hit miss等,显示缓存是否被击中,老版本默认可以,但是新版本,发现需要加上这个)

access_log /var/log/nginx/cache.log cache; #使用这个日志格式


expires 1d;
}

#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
location ~ /purge(/.*)
{
#设置只允许指定的IP或IP段才可以清除URL缓存。
allow 127.0.0.1;
deny all;
#proxy_cache_purge cache_one$host$1$is_args$args;
}

#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*\.(php|jsp|cgi)?$
{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}

access_log off;
}

#真实的图片服务器
server
{
listen 80;

server_name 172.16.69.132;

location /
{
root /;

}

access_log /usr/local/webserver/nginx/logs/nginx_access.log;

}
}

 

#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

 /etc/nginx/sites-enabled/default如下:

# You may add here your
# server {
# ...
# }
# statements for each of your virtual hosts to this file

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

server {
listen 80;
listen [::]:80 ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

location / {
alias /server/;
autoindex on;
allow 127.0.0.1;
#allow 10.43.42.53 #mobile ip
#allow 172.16.69.86 #school ip

# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
#proxy_pass http://localhost:8080;
#}

#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 /usr/share/nginx/html;
#}

# pass the PHP scripts to FastCGI server listening on localhost:9000
#
#location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass localhost:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# root html;
# index index.html index.htm;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}


# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
#
# root html;
# index index.html index.htm;
#
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
#
# ssl_session_timeout 5m;
#
# ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
# ssl_ciphers "HIGH:!aNULL:!MD5 or HIGH:!aNULL:!MD5:!3DES";
# ssl_prefer_server_ciphers on;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

 

posted @ 2015-10-21 10:06  PKICA  阅读(6288)  评论(0编辑  收藏  举报