Ubuntu14.04 安装nginx

===============================解压安装=============================
1、解压到
tar -zxvf nginx-1.6.2.tar.gz -C /home/nginx-1.6.2
cd /home/nginx-1.6.2
sudo ./configure  --prefix=/usr/local/nginx

#报如下错误
./configure: error: the HTTP rewrite module requires the PCRE library.

解决方法:
需要安装pcre包。
    sudo apt-get update
    sudo apt-get install libpcre3 libpcre3-dev
你可能还需要安装 
    sudo apt-get install openssl libssl-dev

######centos######
1、第一种
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel

wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
yum install nginx

whereis nginx

vi /etc/nginx/conf.d/default.conf
######centos######

make
sudo make install

2、第二种
安装prce(重定向支持)和openssl(https支持,如果不需要https可以不安装。)
yum -y install pcre*
yum -y install openssl*
yum -y install zlib*
wget http://nginx.org/download/nginx-1.9.2.tar.gz

tar -zxvf nginx-1.9.2.tar.gz
cd nginx-1.9.2
./configure --prefix=/usr/local/nginx-1.9.2
./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-cc-opt='-03' --with-cpu-opt=opteron
#如果没有error信息,就可以执行下边的安装了
make
make install

#/usr/local/nginx-1.9.2/conf/nginx.conf




===============================直接安装=============================
1、安装nginx
#sudo apt-add-repository ppa:nginx/development
#sudo apt-get update
sudo apt-get install nginx



2、配置
在 Ubuntu 查询 Nginx 配置文件位置?
sudo vim /usr/share/nginx/www/index.html
#sudo find / -name nginx.conf | less
sudo vim /etc/nginx/nginx.conf

我们可以看到nginx文件夹内有一个conf文件夹,其中有好几个文件,其他先不管,我们打开nginx.conf,可以看到一段:



这段代码在server里面,相当于一个代理服务器,当然可以配置多个。
下面我们仔细来分析一下:
listen:表示当前的代理服务器监听的端口,默认的是监听80端口。注意,如果我们配置了多个server,这个listen要配置不一样,不然就不能确定转到哪里去了。
server_name:表示监听到之后需要转到哪里去,这时我们直接转到本地,这时是直接到nginx文件夹内。
location:表示匹配的路径,这时配置了/表示所有请求都被匹配到这里
root:里面配置了root这时表示当匹配这个请求的路径时,将会在这个文件夹内寻找相应的文件,这里对我们之后的静态文件伺服很有用。
index:当没有指定主页时,默认会选择这个指定的文件,它可以有多个,并按顺序来加载,如果第一个不存在,则找第二个,依此类推。
那我们知道了具体的配置了,怎么让它访问localhost时转到tomcat时。实际上就修改两个地方:
server_name localhost:8080;  
  
location / {  
    proxy_pass http://localhost:8080  
}

#多个url
        server_name localhost:8080 localhost:8081;

        location / {
           proxy_pass http://localhost:8080;
        }

        location /nexus {
           proxy_pass http://localhost:8081;
        }

我们就修改了上面两个地方,我的tomcat在8080端口,可以根据自己的需要修改。这里有一个新元素proxy_pass,它表示代理路径,相当于转发,而不像之前说的root必须指定一个文件夹。
nginx重新加载
sudo nginx -t
sudo nginx -s reload



我在安装nginx之前已经安装了apahe以及nexus,所以在后面直接加入,如果没有安装的忽略以下部分

        upstream  nexus {
           server 192.168.36.134:8081;
        }

        server {
        listen       80;
        server_name  192.168.36.134;

        location /nexus {
           proxy_pass      http://nexus;
           proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
           proxy_redirect off;
           proxy_buffering 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;
        }


        }


测试配置问题
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful




3、启动nginx
sudo /etc/init.d/nginx start
sudo nginx -s reload

nginx在ubuntu下会被安装成service, 所以相应的起止办法是:
sudo service nginx start|stop|restart


==============================================================
卸载方法1.
sudo apt-get remove nginx # 删除nginx,保留配置文件
rm -rf /etc/nginx #删除配置文件



卸载方法2.
sudo apt-get purge nginx #删除nginx连带配置文件
sudo apt-get autoremove #卸载不再需要的nginx依赖程序

猜你喜欢

转载自jinjzk.iteye.com/blog/2098762