您的位置:首页 > 运维架构 > Nginx

nginx 安装配置指南

2015-10-12 15:12 676 查看



yum install gccgcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre-devel make–y
建立nginx的系统用户与组(-r :建立指定系统中的用户、组)
groupadd -r nginx

useradd -s /sbin/nologin -g nginx -r nginx
验证
id nginx
配置nginx的模块
./configure --prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_addition_module \
--with-http_stub_status_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-http_secure_link_module \
--with-http_auth_request_module \
--with-http_realip_module
make && make install
启动nginx
/usr/sbin/nginx -c /etc/nginx/nginx.conf
查看服务是否启动
ps -ef | grep nginx
netstat -antulp | grep nginx
启动的时候会报错,是因为刚才自己手动指定了临时目录,所以这时你要自己手动创建一个这个目录即可。
mkdir /var/tmp/nginx/client -pv
nginx -c /etc/nginx/nginx.conf
停止nginx
从容关闭QUIT
kill -QUIT $(cat /var/run/nginx/nginx.pid)
快速停止
kill -TERM $(cat /var/run/nginx/nginx.pid)
强制结束所有的nginx进程
kill -9 nginx
平滑重启nginx
kill -HUP $(cat /var/run/nginx/nginx.pid)
/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
可以把rpm包中的nginx脚本文件复制过来自己修改后再用
rpm2cpio nginx-1.9.0.rpm | cpio -div 解压rpm包
cp nginx /etc/init.d/
chmod a+x /etc/init.d/nginx
chkconfig --add nginx
chkconfig --list nginx
修改/etc/init.d/nginx脚本文件,否则服务无法用这个脚本启动,因为在安装的过程中,这些都是自己指定的文件和路径。(已修改)
prog=nginx
nginx=${nginx-/usr/sbin/nginx}
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/nginx.lock}
pidfile=${PIDFILE-/var/run/nginx/nginx.pid}
SLEEPMSEC=100000
RETVAL=0
编译nginx.conf 详解
worker_processes 2 这里建议有几个cpu就分给它几个进程即可。
worker_connections 1024;设置可以最大的连接请求数(默认1024),超过过就在队列里等待
sendfile on ; 内存分为两段,一段椒user,另一段是kernel,而用户去访问请求时,就会去访问内核,而这个是把访问的这个过程记录下来,放入内存中,而不用每次去内核中读取了。频繁访问东西都会放在内存当中。(是否开启带有缓存的这种机制)
gzip on ; 是否启动压缩功能,在大并发的时候会有所体现。
Location类似于apache的directory,而root旁没写绝对路径那么它的根就是你安装时指定的那个路径。








而这里的/(根)是从/usr/html这里开始的,下面有定义。(从安装路径的html开始)




虚拟主机的配置过程。
我在一个主机里用一个Ip对应多个域名来配置,写在/etc/hosts里




1、虚拟主机配置过程,/etc/nginx/nginx.conf








最后:killall -1 nginx (与service nginx reload结果一样)
建立目录与在目录中写入内容,命令为index.html
mkdir –p/var/www/virtual2
mkdir –p /var/www/virtual
验证:



下面配置文件中会用到大量的正则表达式。









把这个打开



以这个为例,再继续修改配置一下。(warn是日志错误级别)


重启服务,然后日志查看
tailf /var/log/nginx/www.example.com-error.log



403错误:索引文件未找到


然后自己定义错误页面
编辑/etc/nginx/nginx.conf


接着建立目录,和编辑错误页面的内容
mkdir /var/www/error
cd /var/www/error
echo This is error > 40x.html
接着重启服务并验证结果:






首先:还是继续修改我们的这个配置文件,把我们原有的再进行一下改造即可。




服务重启并验证结果



之前我复制过来的这个文件


别名功能:
1、编辑/etc/nginx/nginx.conf 配置文件。



2、建立目录及索引文件



3、重新启动服务



4、验证结果:






没有明确拒绝都是允许所有






编译安装的时候必须指定这个模块,否则是没有这个功能的。
--with-http_stub_status_module
1、编辑 /etc/nginx/nginx.conf



2、重新启动服务



3、验证结果:



这个nginx_status目录之前是不用建立的,按以上操作完成即可。还可以对这个目录进行加密,不让其他人看到这个结果,这个是可以传给类似ngios与cacti来作监控用的一个功能。


























nginx.conf 配置
user wwwroot;
worker_processes auto;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
server_tokens off;
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 1024;
variables_hash_max_size 1024;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 120;
client_max_body_size 50m;
gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_comp_level 9;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/xml text/css text/comma-separated-values
text/javascript application/x-javascript application/atom+xml
image/jpeg image/gif image/png image/jpg;
gzip_disable "MSIE [1-6]\.";
#open_file_cache max=102400 inactive=20s;
#open_file_cache_valid 60s;
#open_file_cache_min_uses 5;
#open_file_cache_errors off;
fastcgi_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=DEDECMS:500m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
geoip_country /usr/local/nginx/conf/GeoIP.dat;
fastcgi_connect_timeout 3600;
fastcgi_send_timeout 3600;
fastcgi_read_timeout 3600;
fastcgi_intercept_errors on;
server {
listen 80;
server_name localhost;
return 500;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name .example.com;
access_log logs/example.access.log main;
location / {
root html;
index index.html index.htm;
}
}
include yw.conf;
}
nginx 地址重写规则 泛解析
##start .example.com##

server {
listen 80;
server_name_in_redirect off;
server_name .example.com;
access_log logs/www.example.com.access.log main;
error_log logs/www.example.com.error.log;
include gen.conf;
if ($host = 'example.com' ) {
rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}
if ($host ~* (\b(?!www\b)[a-zA-Z0-9\-]+).example.com$) {
set $rs1 /$1;
}
set $rootdir /alidata1/web/html/www.example.com$rs1;
root $rootdir;
location / {
root $rootdir;
expires 1M;
add_header Cache-Control "public";
index index.html index.htm index.html index.php default.html Default.html;
}

location ~ .*\.(php|php5)?$ {
root $rootdir;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $rootdir$fastcgi_script_name;
include fastcgi_params;
}

error_page 404 /404.html;
location = /404.html {
root /alidata1/web/html/www.example.com;
}
}

##end .example.com##

没有地址重写
##start www.example.com##

server {
listen 80;
server_name_in_redirect off;
server_name www.example.com;
access_log logs/www.example.com.access.log main;
error_log logs/www.example.com.error.log;
set $rootdir /home/wwwroot/www.example.com;
include gen.conf;
location / {
root $rootdir;
expires 1M;
add_header Cache-Control "public";
index index.html index.htm index.html index.php default.html Default.html;
}

location ~ .*\.(php|php5)?$ {
root $rootdir;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $rootdir$fastcgi_script_name;
include fastcgi_params;
}

error_page 404 /404.html;
location = /404.html {
root /home/wwwroot/www.example.com;
}

}

##end example.com##

全部反向代理 nginx.conf
upstream proxyserver {
server 114.80.80.41:80 fail_timeout=1s max_fails=1;
server 103.6.222.85:80 backup;
keepalive 20;
}
include proxy.conf;
proxy.conf

server {
server_name ~^([^\.]+)\.([^\.]+)$ ~^([^\.]+)\.([^\.]+)\.([^\.]+)$ ~^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)$;
server_name_in_redirect off;
access_log logs/access.all.log main;
error_log logs/error.all.log;
location / {
proxy_pass http://114.80.80.41; proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Cache $upstream_cache_status;
expires 1M;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
server {
server_name google.com www.google.com g.cn www.g.cn;
server_name_in_redirect off;
access_log logs/g.all.log main;
error_log logs/g.all.log;
location / {
proxy_pass https://www.google.com/?gws_rd=ssl; proxy_redirect off;

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Cache $upstream_cache_status;
expires 1M;

client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: