nginx安装及使用

mac中nginx的安装及配置

1. brew install nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ brew install nginx 

==> nginx
Docroot is: /usr/local/var/www

The default port has been set in /usr/local/etc/nginx/nginx.conf to 8080 so that
nginx can run without sudo.

nginx will load all files in /usr/local/etc/nginx/servers/.

To have launchd start nginx now and restart at login:
brew services start nginx
Or, if you don't want/need a background service you can just run:
nginx

2. 运行nginx

1
2
3
4
5
$ brew services start nginx
$
$
$ nginx -s stop
$ nginx -s reload

访问 http://localhost:8080

3. 配置nginx

1
$ cd /usr/local/Cellar/nginx/1.17.8/

/usr/local/etc/nginx/nginx.conf

3.1. 代理配置

路径中存在eduservice 转至 http://localhost:8001 tomcat运行路由

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 9500;
server_name localhost;

location / {
root html;
index index.html index.htm;
}

location ~ /eduservice/ {
proxy_pass http://localhost:8001;
}
}

3.2. 启动异常

1
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)

可以直接启动nginx,重新生成nginx.pid就可以了:

1
$ nginx

如果直接启动还是不可行,执行nginx -t查看nginx配置文件路径:

1
2
3
$ nginx -t
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful

指定一下conf文件:

1
$ nginx -c /usr/local/etc/nginx/nginx.conf

再次重启nginx -s reload,就不会报错了

3.3. nginx实现反向代理

实现分布式下负载均衡

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http{
upstream tomcat{
server 192.168.0.112:8080 weight=20;
server 192.168.0.112:8081 weight=20;
server 192.168.0.112:8082 weight=20;
}
server{
listen 9000;
server_name localhost;
location /{
proxy_pass http://tomcat;
}
}
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/11/07/nginx安装及使用/
  • 发布时间: 2019-11-07 14:04
  • 更新时间: 2022-10-22 19:54
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!