nginx安装、使用 入门
以下示例使用mac操作,
windows用户可以根据实际情况查找nginx.config路径
1.安装
brew install nginx
如果报错:
zsh: command not found:brew
那证明你没有安装brew
需要安装一下
brew 是 Mac 下的一个包管理工具,类似于 centos 下的 yum,可以很方便地进行安装/卸载/更新各种软件包,例如:nodejs, elasticsearch, kibana, mysql, mongodb 等等,可以用来快速搭建各种本地环境,程序员必备工具
点这这传送门,看 brew安装教程
2.配置config文件
文件位置: /usr/local/etc/nginx/nginx.conf
可以使用编辑器打开,然后配置所需参数
以下是可选参数,不一定要全放进去
#user nobody;
# 设置访问权限( user root owner; ),不然等会访问网站会出现403错误
user root 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;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8282;
server_name localhost;#这里也可以填写自己的域名 或者服务器地址 比如104.224.166.36
#charset koi8-r;
#access_log logs/host.access.log main;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
location /proxy/ {
# root html/newdailian; #表示服务器文件根目录 项目放在此目录下面
# if (!-e $request_filename) {
# rewrite ^/index.php/(.*)$ /newdailian/index.php?s=$1 last;
# break;
# }
proxy_pass http://tp5.test.com:7888
index index.html index.htm index.php;
}
#打开这部分
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
#打开这部分配置
# PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI协议默认配置.
# Fastcgi服务器和程序(PHP,Python)沟通的协议.
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$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;
# }
#}
include servers/*;
}
打开文件后有一些最基础的配置是有的
最主要的是要配置 http下的 server
基础结构是这样的
server{
listen 8082; # listen很明显是配置端口号的,可以自己随意配置
server_name localhost; #这里也可以填写自己的域名 或者服务器地址
location /proxy/ {
root html/newdailian; #表示服务器文件根目录 项目放在此目录下面
index index.html index.htm index.php; #这里是配置启动服务默认打开的页面,可以配置一个也可以是多个
}
注意上面第4行 user root owner 这句话
你可以不加
但是据说会报403
如果遇到了这个问题你可以加一下
以下命令应当在nginx安装目录sbin下输入
// 例如在 sbin 下执行 重启nginx服务
// 注意 nginx 命令需要指定到路径
// 如下 写成 ./nginx
./nginx -s reload
3.启动
nginx -c /usr/local/etc/nginx/nginx.conf
4.重启
nginx -s reload
5.退出
nginx -s quit
6.关闭(停止)
nginx -s stop
7.测试nginx配置文件是否正确
nginx -t -c /usr/local/etc/nginx/nginx.conf
处理报错:
以上报错是权限问题
使用mac操作会出现这个错误
命令前面要加 sudo
最后输入开机密码就可以了
本文配置参考整合来自: nginx配置反向代理