首先nginx 安装目录
sudo vim /usr/local/etc/nginx/nginx.conf
如果大家不太会使用vim命令建议使用vscode打开配置文件,方法:快捷键 command+shift+p 选择如下图
选择完成后 将上述命令行改为:
code /usr/local/etc/nginx/nginx.conf
先说下配置nginx最重要的三点
1.server : server就是服务器的站点,一个server就代表着一个域名站点如https://www.baidu.com,一个站点又可以配置多个项目,而配置多个项目时则需要配置多个root 路径来指定服务器从哪里找到这个项目。
2.server_name: 域名,这个域名可以是你在云服务器上申请的域名,也可以是本地域名,localhost,不同的server域名也需要不同,但如果想要在本地设置多个server怎么办,就需要在/etc/hosts文件下 设置多个不同的域名指向,如 127.0.0.1 localvue 即可。
3 root: 项目目录地址,不同的server之间,直接设置不同的root就行了,但是要在同一server下,根据路径不同,指定不同的目录怎么办.看如下代码
#此 localhost 只的是根域名 下指向的路径
# 如果想配置前后端项目在同一域名下 则需将根域名下的项目指向到前端项目
# 因为一般后端项目抖音前缀api 所以直接指向域名的二级目录不会出现问题
if ($host ~* ^localhost$) {
set $rootdir "/Users/gatsbyzz/myProject/vue/dist";
}
# 同一站点不同项目 需配合php项目路由,或vue项目更改前缀
# 如下 访问后端项目路径则是 localhost/api
# if ($request_uri ~* ^/api/?.*) {
# set $rootdir "/Users/gatsbyzz/myProject/phpgit/public";
# }
root $rootdir;
上述root的配置是最重要的 需要好好理解。
其他就是些php的配置, 这些都是可以搜到的。
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
最后如果上述都配置完了 出现file not found 怎么办(当时困扰我很久)其实是因为文件读取权限问题, 只需要 chmod -R 777 【dir/】解决权限问题了。
nginx over
tips:nginx 命令
sudo nginx
#重启
sudo nginx -s reload
再附一张一个完整配置代码
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;
index index.html index.htm index.php;
#access_log logs/host.access.log main;
if ($host ~* ^localhost$) {
set $rootdir "/Users/gatsbyzz/myProject/phpgit/public";
}
# 同一项目不同站点 需配合php项目路由更改前缀
# if ($request_uri ~* ^/phpgit/?.*) {
# set $rootdir "/Users/gatsbyzz/myProject/phpgit/public";
# }
# if ($request_uri ~* ^/vue/?.*) {
# set $rootdir "/Users/gatsbyzz/myProject/vue/dist";
# }
root $rootdir;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
#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$ {
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;
}
}
# zerg
# 解决file not found问题
# chmod -R 777 【dir/】
server {
listen 80;
server_name localphp;
index index.html index.htm index.php;
if ($host ~* ^localphp$) {
set $rootdir "/Users/gatsbyzz/myProject/zerg/public";
}
root $rootdir;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name api-operation.test;
index index.html index.htm index.php;
if ($host ~* ^api-operation.test$) {
set $rootdir "/Users/gatsbyzz/workOnAoya/api-operation/public";
}
root $rootdir;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# 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/*;
}