美文网首页
Mac配置Nginx

Mac配置Nginx

作者: KamyShi | 来源:发表于2020-04-14 11:39 被阅读0次

Nginx配置

局域网内反向代理服务器

  之前和前端调试的时候,用手机调试很不方便,就在本地搭建了一个Nginx用来处理本机跑起来的前端项目,这样就可以把本机当做一个服务器进行访问了。

1、下载安装Nginx

brew install nginx

2、配置Nginx

1、进入nginx的安装目录,打开nginx.conf文件

cd /usr/local/etc/nginx/
sudo vim nginx.conf

2、进行配置,修改访问端口和服务对应地址

其中主要修改了server里面的内容,server.listen 为端口号,此处我修改为8002;

location则为服务链接对应的配置,可以设置路由对应本地地址,也可以使用proxy_pass设置转接。后面我添加了一个server,设置4000端口,则是使用proxy_pass做了npm的服务中转。


#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  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;

    server {
        listen       8002;
        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;
        #}
    }

    server {
        listen       4000;
        server_name  localhost;
        location / {
            proxy_pass http://127.0.0.1:3001;
        }
    }


    # 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;
    #    }
    #}
    include servers/*;
}

3、启动服务,测试

上面的配置中默认配置的server,我修改端口号为8002,新增了一个为4000,此处启动Nginx,测试8002是否可用即可。

  • 启动nginx:sudo nginx
  • 重启nginx:sudo nginx -s reload
  • 查看nginx进程:ps -ef | grep nginx
  • 杀死进程 kill [[pid]

启动Nginx,浏览器打开localhost:8002,展示Nginx的欢迎页,则启动成功。

Tip:参考:https://jingyan.baidu.com/article/0eb457e5df767c03f1a90533.html
https://www.jianshu.com/p/dd69d6243646
https://www.jianshu.com/p/4387ef52681f

相关文章

网友评论

      本文标题:Mac配置Nginx

      本文链接:https://www.haomeiwen.com/subject/sgezmhtx.html