ubuntu中的操作
$sudo /etc/init.d/nginx start //启动
$sudo /etc/init.d/nginx stop //停止
$sudo /etc/init.d/nginx restart //重启
//配置文件/etc/nginx/nginx.conf
#/usr/local/nginx/conf/nginx.conf
安装依赖
1、模块依赖性
gzip 模块需要
zlib 库 rewrite 模块需要
pcre 库 ssl 功能需要
openssl 库 预先编译好的安装包
sudo apt-get install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev
命令安装
#下载nginx 保存到download目录
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxvf nginx-1.6.2.tar.gz
cd nginx-1.6.2
./configure --user=wwwuser --group=wwwgroup --prefix=/usr/local/nginx/ --with-http_ssl_module
#这里说一下几个参数的意思,--user 所有者 --group 所属组 --prefix 安装路径 --with-http_ssl_module开启https模块,默认是关闭的
make & make install
#默认安装目录 /usr/local/nginx 或 whereis nginx 查询#以下命令供使用
#启动
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
#重载配置文件
/usr/local/nginx/sbin/nginx -s reload
#停止
/usr/local/nginx/sbin/nginx -s stop
#检查配置文件语法
/usr/local/nginx/sbin/nginx -t
#启动成功后测试下是否成功
查看端口或者curl
lsof -i:80
#查看下端口curl 127.0.0.1
nginx反向代理和负载均衡
最简陋的配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 6666;
}
http {
#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;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
upstream myNodejs{
ip_hash;
server 127.0.0.1:3001 weight=3;
}
upstream myPython{
ip_hash;
server 127.0.0.1:8888 weight=3;
}
server {
listen 80;
listen 443;
server_name node.boqiao919.com;
ssl on;
ssl_certificate cert/213991280040065.pem;
ssl_certificate_key cert/213991280040065.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://myNodejs;
}
}
server {
listen 80;
listen 443;
server_name python.boqiao919.com;
ssl on;
ssl_certificate cert/213991280040065.pem;
ssl_certificate_key cert/213991280040065.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://myPython;
}
}
server {
listen 80;
listen 443;
server_name boqiao919.com;
ssl on;
ssl_certificate cert/213991280040065.pem;
ssl_certificate_key cert/213991280040065.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{
root /home/liyang/nginxStatic;
expires 15d;
}
}
}
eg:
#user nobody;
worker_processes 1;
events {
worker_connections 6666;
}
http {
gzip on;
upstream myNodejs{
ip_hash;
server 127.0.0.1:3001 weight=3;
}
upstream myPython{
ip_hash;
server 127.0.0.1:8888 weight=3;
}
server {
listen 80;
server_name node.boqiao919.com;
location / {
proxy_pass http://myNodejs;
}
}
server {
listen 80;
server_name node.boqiao919.com;
location / {
proxy_pass http://myPython;
}
}
}
eg:
#设置用户(如果设置为root,那么代表就是只有root用户才可以使用)
#user nobody;
#工作衍生进程数,一般设置为cpu的核数或者cpu核数的2倍
worker_processes 1;
#设置错误文件存放路径(notice和info代表一种错误)
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#设置pid存放路径(pid是控制系统中的重要文件)
#pid logs/nginx.pid;
#设置最大连接数
events {
worker_connections 1024;
}
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;
#keepalive_timeout 0;
keepalive_timeout 65;
#是否开启压缩
#gzip on;
#设置压缩下限
#gzip_min_lenth 1k;
#设置压缩文件的内存大小设置
#gzip_buffers 4 16k;
#设置http支持的版本
#gzip_http_version 1.1;
#判断客户端时候支持gzip,支持才会压缩
#gzip_vary on;
#虚拟主机的配置
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# 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;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
网友评论