nginx知識雜記

Mac下nginx的配置文件默認的路徑:

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

找到上述路徑下的nginx配置文件,打開:


user lizuncong owner;
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;
    fastcgi_intercept_errors on;

    proxy_intercept_errors on;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    #include sites-enabled/*.conf;
    server {
        listen       8086;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location / {
            root   html;
            index  index.html index.htm;
         }
	
	# add by lzc 
	# location / {
	#     proxy_pass http://localhost:8001; 
	# }
	
	# add by lzc
	location /api/ {
	    proxy_pass http://localhost:8002;
	    proxy_set_header Host $host;
	}

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

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # include /usr/local/etc/nginx/conf.d/*.conf;
     include /usr/local/etc/nginx/sites-enabled/*;
     include /Users/lizuncong/Documents/nginx-service/index.conf;
   # include sites-enabled/690-back;

}

修改第一行,添加當前用戶user lizuncong owner;

設置代理,比如,如果想將/路徑下的服務都轉發到http://localhost:8001服務下,而將/api/路徑下的接口都轉發到http://localhost:8002服務下,那麼可以這樣配置:

	# add by lzc 
	location / {
	    proxy_pass http://localhost:8001; 
	}
	
	# add by lzc
	location /api/ {
	    proxy_pass http://localhost:8002;
	    proxy_set_header Host $host;
	}

配置文件裏面還可以引入另外的配置文件,比如這裏,引入了外部index.conf配置。

     include /Users/lizuncong/Documents/nginx-service/index.conf;

index.conf配置如下:

server
{
  charset utf-8;
  listen 8085;
  server_name http_host;
  root /Users/lizuncong/Documents/nginx-service/; 
  autoindex on;
  add_header Cache-Control "no-cache, must-revalidate"; 
  location / {
    add_header Access-Control-Allow-Origin *;
  }
}

測試配置文件格式是否正確:

nginx -t

啓動nginx,直接在命令行運行nginx就可以啓動:

nginx

重啓nginx

nginx -s reload

停止nginx

nginx -s stop

以上這些命令在mac上可能需要加上sudo權限。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章