nginx安装及使用-MacOS

文章目录

  • brew install nginx
  • 运行nginx
  • 配置nginx
    • 代理配置
    • 启动异常
    • nginx实现反向代理

brew install nginx

$ 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

运行nginx

$ brew services start nginx
$ 
$
$ nginx -s stop
$ nginx -s reload

访问 http://localhost:8080

配置nginx

nginx安装及使用-MacOS_第1张图片

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

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

代理配置

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

server {
        listen       9500;
        server_name  localhost;   
        
        location / {
            root   html;
           index  index.html index.htm;
        }

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

启动异常

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

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

$ nginx

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

$ 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文件:

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

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

nginx实现反向代理

请求都转发到tomcat,实现分布式下负载均衡

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 192.168.0.112:8083 weight=20;
	}
 server{
   listen       9000;
   server_name  localhost;
   location /{
      proxy_pass http://tomcat;
   }
 }
}

你可能感兴趣的:(mac,java)