nginx 日常使用文档

安装

brew install nginx

命令

sudo nginx

sudo nginx -s reload

sudo niginx -s stop

lsof -i :端口

kill -9 进程ID

目录

/usr/local/etc/nginx

常用文件

nginx.conf

servers(api.fe-newd2c.sankuai.com.conf, fe-newd2c.sankuai.com.conf)

hosts修改

sudo vi /etc/hosts

127.0.0.1 api.fe-newd2c.sankuai.com fe-newd2c.sankuai.com  
// 127.0.0.1 nginx都是监听127.0.0.1的80端口,所以一般不会变

nginx 配置

fe-newd2c.sankuai.com.conf


server {
    listen 80;
    server_name fe-newd2c.sankuai.com;
    location / {

        proxy_pass http://127.0.0.1:4118;
        client_max_body_size 200m;
    }
}

api.fe-newd2c.sankuai.com.conf


server {
    listen 80;
    server_name api.fe-newd2c.sankuai.com;
    location / {
        proxy_set_header Host $http_host;

        proxy_pass http://127.0.0.1:3225;
        client_body_timeout 300s;
        client_max_body_size 200m;
    }
}

nginx反向代理简易说明图

同时nginx可以在本地代理做跨域(有时候会遇到后台说自己框架限制,不好设置跨域)

常见问题

nginx已经启动,想要关闭重新启动,却显示kill失败.

解决方法

mac OS:

查看端口:lsof -i :端口
杀死进程:kill -9 进程ID

linux :

查看端口: netstat -an | grep 端口

查看nginx日志解决nginx问题

举例:本地数据图表项目,复杂数据无法上传通过查看nginx日志显示failed (13: Permission denied):

发现报错误是:failed (13: Permission denied):

googole查询:https://my.oschina.net/githubhty/blog/1632342

然后通过sudo nginx 解决

备注:nginx.conf 文件

#user root;
worker_processes  1;

error_log  /usr/local/var/log/nginx/error.log debug;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  256;
}


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  /usr/local/var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;
    gzip_min_length 1000;
    gzip_types text/plain application/xml text/css application/x-javascript;

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


    # 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;
    #    }
    #}
     add_header Cache-Control no-cache;
     proxy_buffering off;
     include servers/*;
}

你可能感兴趣的:(nginx 日常使用文档)