目录:
1.安装
2.使用
3.遇到的坑
详情:
1.安装nginx:
@ brew search nginx
@ brew install nginx
@ 验证是否成功:nginx -v
@ 启动:sudo nginx
@ 重新启动:sudo nginx -s reload
@ 快速关闭 nginx -s stop
@ 优雅的关闭 nginx -s quit
2.nginx反向代理使用
nginx 及其模块的工作由配置文件定义,默认的配置文件为 nginx.conf,该配置文件可能位于:/usr/local/nginx/conf, /etc/nginx, or /usr/local/etc/nginx
--------nginx.conf代码
user www www;
worker_processes 2;
error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 2048;
}
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 65;
# gzip压缩功能设置
# http_proxy 设置
# 设定负载均衡后台服务器列表
upstream backend {
#ip_hash;
server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
}
# 很重要的虚拟主机配置
server {
listen 80;
server_name itoatest.example.com;
root /apps/oaapp;
charset utf-8;
access_log logs/host.access.log main;
#对 / 所有做负载均衡+反向代理
location / {
root /apps/oaapp;
index index.jsp index.html index.htm;
proxy_pass http://backend;
proxy_redirect off;
# 后端的Web服务器可以通过X-Forwarded-For获取用户真实IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
#静态文件,nginx自己处理,不去backend请求tomcat
location ~* /download/ {
root /apps/oa/fs;
}
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
{
root /apps/oaapp;
expires 7d;
}
location /nginx_status {
stub_status on;
access_log off;
allow 192.168.10.0/24;
deny all;
}
location ~ ^/(WEB-INF)/ {
deny all;
}
#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;
}
}
# 在servers文件中创建新service文件并include进来
include servers/*;
## 其它虚拟主机,server 指令开始
}
在nginx.conf中反向代理最重要的指令是 proxy_pass;并且通过 location 匹配 url 路径,将其转发到另外一个服务器处理。
-------Nginx入门之静态资源与动态访问分离
参考:https://blog.csdn.net/jiachengwin/article/details/75106861
3.遇到的问题
- nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
解决:我设定的8080端口被占用,我这边是关掉tomcat - nginx映射PHP文件,导致PHP文件下载而非打开网页
解决:下载PHP-fpm, 设置fastcgi配置
location ~ \.php$ {
root XXX;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
参考:https://blog.csdn.net/dengjiexian123/article/details/53358452
解释:
-- 1、location ~ .php?.*${ }代表一个能匹配对应uri的location
-- 2、FastCGI 是一个协议,它是应用程序和 WEB 服务器连接的桥梁。Nginx 并不能直接与 PHP-FPM 通信,而是将请求通过 FastCGI 交给 PHP-FPM 处理, fastcgi_pass 就是把所有 php 请求转发给 php-fpm 进行处理。通过 netstat -nplt 命令可以看到,127.0.0.1:9000 这个端口上运行的进程就是 php-fpm.
-- 3、fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Here's what the documentation says:
a. $request_filename
This variable is equal to path to the file for the current request, formed from directives root or alias and URI request;
b. $document_root
This variable is equal to the value of directive root for the current request
c. $fastcgi_script_nameThis variable is equal to the URI request or, if if the URI concludes with a forward slash, then the URI request plus the name of the index file given by fastcgi_index. It is possible to use this variable in place of both SCRIPT_FILENAME and PATH_TRANSLATED, utilized, in particular, for determining the name of the script in PHP.
-- 4、include fastcgi_params; 引入fastcgi配置文件
- 安装php.npm遇到的问题
--- 1.
$ php-fpm
[11-Jan-2014 16:03:03] ERROR: failed to open configuration file '/private/etc/php-fpm.conf': No such file or directory (2)
[11-Jan-2014 16:03:03] ERROR: failed to load configuration file '/private/etc/php-fpm.conf'
[11-Jan-2014 16:03:03] ERROR: FPM initialization failed
解决:cp /private/etc/php-fpm.conf.default /usr/local/etc/php-fpm.conf
--- 2.
$ php-fpm --fpm-config /usr/local/etc/php-fpm.conf
[11-Jan-2014 16:10:49] ERROR: failed to open error_log (/usr/var/log/php-fpm.log): No such file or directory (2)
[11-Jan-2014 16:10:49] ERROR: failed to post process the configuration
[11-Jan-2014 16:10:49] ERROR: FPM initialization failed
解决:原因是默认在/usr/var目录下工作,可以修改配置文件指定正确的日志文件路径;因此打开$ vim /usr/local/etc/php-fpm.conf,去掉error-log前面的;,并且修改为error_log = /usr/local/var/log/php-fpm.log,再运行sudo php-fpm即可