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权限。