nginx

Linux 安装nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash

#Nginx是使用C语言开发,安装nginx需要先从官网上将源码下载,然后编译,编译需要gcc环境:
yum install -y gcc-c++

#PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库
yum install -y pcre pcre-devel

#zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库
yum install -y zlib zlib-devel

#OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
#nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel



wget -P ~/soft/ "http://ou0y29lb2.bkt.clouddn.com/nginx-1.8.0.tar.gz"

if [ ! -d "/usr/local/nginx" ]; then
mkdir /usr/local/nginx
fi

cp ~/soft/nginx-1.8.0.tar.gz /usr/local/nginx

cd /usr/local/nginx

tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0


#使用nginx的configure命令进行配置,./configure --help查询详细参数
#进行nginx-1.8.0目录执行以下命令(其中prefix参数指定的是nginx的安装目录):

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

mkdir -p /var/temp/nginx/

make && make install

echo ■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■

ls -al /usr/local/nginx

启动 nginx

1
2
3
/usr/local/nginx/sbin/nginx

ps -ef | grep nginx

重新加载配置文件

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,
使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,
如下:

1
/usr/local/nginx/sbin/nginx –s reload

nginx停止

1
2
3
快速停止
# 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
/usr/local/nginx/sbin/nginx –s stop
1
2
3
完整停止(建议使用)
# 此方式停止步骤是待nginx进程处理任务完毕进行停止
/usr/local/nginx/sbin/nginx –s quit



mac 安装 nginx

1
brew install nginx
1
2
3
/usr/local/etc/nginx/nginx.conf (配置文件路径)
/usr/local/var/www (服务器默认路径)
/usr/local/Cellar/nginx/1.13.8 (安装路径)

Alt text

mac的configure

Alt text

启动

brew 已经讲命令放到 /usr/local/bin/nginx, 可以直接执行

1
nginx

Mac下判断配置文件是否正确

1
nginx -t

Mac下重启Nginx

1
nginx -s reload

nginx停止

1
2
3
快速停止
# 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
nginx –s stop
1
2
3
完整停止(建议使用)
# 此方式停止步骤是待nginx进程处理任务完毕进行停止
nginx –s quit

关闭进程

1
ps -ef | grep nginx | grep -v grep | awk '{print $2}' | xargs kill -9

nginx重启

先停止再重启

对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
如下:

1
2
nginx –s quit
nginx

重新加载配置文件
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

1
nginx –s reload

在 Sublime Text 中使用 SFTP 插件快速编辑远程服务器文件 Sublime-text with SFTP plugin

http://justcoding.iteye.com/blog/2029825


坑1: 修改hosts文件不起作用

使用普通用户开启 nginx

配置 拦截80端口 解析自定义域名时遇到

1
ngix -t

结果:

1
2
3
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed

【Nginx】nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

这是端口号的问题,在Linux中1024以下的端口号都需要root权限才能使用,所以普通用户启动程序绑定会报出权限问题。

使用如下命令启动nginx。

解决办法:
关闭nginx , 用管理员启动nginx

1
sudo nginx

1
sudo nginx -t

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
ginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

坑2: 访问不了html文件之外的资源文件(如 图片等)

chmod -R +rx css

chmod -R +rx js

至少得有读的权限…蛋疼…

Alt text


sudo 不输入密码

echo 密码 | sudo -S nginx -t


反向代理配置

1
2
3
4
5
6
7
8
server {
listen 80;
server_name www.tomcat.com; # 用户通过80端口访问

location / {
proxy_pass http://localhost:8080; # 去局域网找数据 找到并返回
}
}

负载均衡配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 反向代理
upstream www.tomcat_server.com {

server localhost:8080 weight=2; #增加权重命中率高,默认都是1
server localhost:8082;
server localhost:8083;
server localhost:8084;
}

server {
listen 80;
server_name www.tomcat_customer.com;

location / {
proxy_pass http://www.tomcat_server.com;
}
}

Nginx配置文件详细说明

http://www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html