Nginx是什么?
Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。
由于手里是macbook,所以从网上找mac上装nginx的教程,网上大部分文章实在不敢恭维,踩了太多坑,将最终可行的方法记录下来,以供大家借阅。
本教程主要介绍如何在 macOS 下搭建 Nginx 服务器并进行相关配置。
1. 安装 Homebrew
Homebrew 是 macOS 下的套件管理器,我们需要使用它进行 Nginx 的安装。
获取Homebrew十分简单,只需要打开终端,输入如下一行代码即可:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2. 安装 Nginx
1)使用 Homebrew 安装 Nginx,在终端输入如下命令:brew install nginx
2)安装后我们使用 nginx -v 来查看 Nginx 版本,若显示如下信息则证明安装成功。
nginx version: nginx/1.15.5
3. 配置 Nginx
1)进入 Nginx 的安装目录,我本地的安装目录是 /usr/local/etc/nginx/ ,打开该目录下的 nginx.conf 文件。
可根据如下命令操作。
cd /usr/local/etc/nginx/
sudo vi nginx.conf
2)对 nginx.conf 文件进行配置:
----------
#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 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root 代表反向代理的文件夹
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;
# }
#}
#实现图片的回显 配置图片服务器
server {
listen 80;
server_name image.jt.com;
location / {
root /Users/stephenshen/Software/jt/jt-upload;
}
}
}
4. nginx使用命令
启动命令:sudo nginx
重启命令:sudo nginx -s reload
快速停止命令:sudo nginx -s stop
平稳退出命令:sudo nginx -s quit