ubuntu14.04安裝與配置nginx服務器

去年曾經配置過nginx服務器,惋惜的是,幾個月前因故障磁盤被格式化。今天又要用到nginx服務,因此重新配置了一番,但此次就不是那麼順利了。在此,願與你們分享一下經驗。只是簡單的局域網應用,並未複雜配置。javascript

命令行安裝:php

$sudo apt-get install nginxcss

若是缺乏依賴包,能夠$sudo apt-get update以後再執行上面的命令。html

 

如下是排錯:java

$ 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/defaultnode

nginx。confnginx

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

pid /run/nginx.pid;json

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;# }#}

相關文章
相關標籤/搜索