1. Nginx 是什么?
Nginx 是一款轻量级Web服务器,也是一款反向代理服务器
2. Nginx 能干什么
可以直接支持Rails 和PHP 程序
可以作为HTTP反向代理
作为负载均衡服务器
作为邮件代理服务器
帮助前端实现动静分离
3. Nginx 特点
高稳定
高性能
资源占用少
功能丰富
模块化结构
支持热部署
4. Nginx 安装
(1) 安装gcc (yum install gcc)
备注:可以输入gcc -v 查询版本信息,看系统是否自带安装
(2) 安装pcre (yum install pcre-devel)
(3) 安装zlib (yum install zlib zlib-devel)
(4) 安装openssl (yum install openssh openssh-devel)
(5) 下载源码包,选择稳定版本(http://www.nginx.org/)
wget http://nginx.org/download/nginx-1.10.2.tar.gz
tar -zxvf nginx-1.10.2.tar.gz
(6) 进入nginx目录之后执行./configure
也可以指定安装目录,增加参数--prefix=/usr/nginx
如果不指定路径,可以通过whereis nginx进行查询
默认安装在/usr/local/nginx
(7) 继续执行make
(8) 继续执行make install
5. nginx 常用命令
(1). 测试配置文件
安装目录下的/nginx/sbin/nginx -t
(2). 启动命令
安装路径下的/nginx/sbin/nginx
(3).停止命令
安装路径下的/nginx/sbin/nginx -s stop 或者 nginx -s quit
(4). 重启命令
安装路径下的/nginx/sbin/nginx -s reload
(5). 查看进程命令
ps -ef | grep nginx
(6). 平滑重启
kill -HUP {Nginx 主进程号(既查看进程命令查到的PID)}
(7). 增加防火墙权限
sudo vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
sudo service iptables restart
6. 虚拟域名配置及测试验证
(1)编辑sudo vi /usr/local/nginx/conf/nginx.conf
(2)增加include vhost/*.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 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; #} } ##########################vhost##################################### include vhost/*.conf; # 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; # } #} }
(3)在/usr/local/nginx/conf/ 目录新建vhost文件夹,既:/usr/local/nginx/conf/vhost
(4)创建域名转发配置文件,点击查看详情
admin.happymmall.com.conf
server { listen 80; autoindex on; server_name admin.happymmall.com; access_log /usr/local/nginx/logs/access.log combined; index index.html index.htm index.jsp index.php; #root /devsoft/apache-tomcat-7.0.73/webapps/mmall; #error_page 404 /404.html; if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; } location = / { root /product/front/mmall_admin_fe/dist/view; index index.html; } location ~ .*\.(html|htm)$ { root /product/front/mmall_admin_fe/dist/view; index index.html; } location / { proxy_pass http://127.0.0.1:8080/; add_header Access-Control-Allow-Origin '*'; } }
happymmall.com.conf
server { listen 80; autoindex on; server_name happymmall.com www.happymmall.com; access_log /usr/local/nginx/logs/access.log combined; index index.html index.htm index.jsp index.php; if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; } location = / { root /product/front/mmall_fe/dist/view; index index.html; } location ~ .*\.html$ { root /product/front/mmall_fe/dist/view; index index.html; } location / { proxy_pass http://127.0.0.1:8080/; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ { proxy_pass http://127.0.0.1:8080; expires 30d; } location ~ .*\.(js|css)?$ { proxy_pass http://127.0.0.1:8080; expires 7d; } }
img.happymmall.com.conf
server { listen 80; autoindex off; server_name img.happymmall.com; access_log /usr/local/nginx/logs/access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /404.html; if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; } location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* { deny all; } location / { root /product/ftpfile/img/; add_header Access-Control-Allow-Origin *; } }
learning.happymmall.com.conf
server { default_type 'text/html'; charset utf-8; listen 80; autoindex on; server_name learning.happymmall.com; access_log /usr/local/nginx/logs/access.log combined; index index.html index.htm index.jsp index.php; #error_page 404 /404.html; if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; } location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* { deny all; } location / { root /product/ftpfile/learning/; add_header Access-Control-Allow-Origin *; } }
s.happymmall.com.conf
server { listen 80; autoindex off; server_name s.happymmall.com; access_log /usr/local/nginx/logs/access.log combined; index index.html index.htm index.jsp index.php; if ( $query_string ~* ".*[\;'\<\>].*" ){ return 404; } location ~ /(mmall_fe|mmall_admin_fe)/dist/view/* { deny all; } location / { root /product/front/; add_header Access-Control-Allow-Origin *; } }
(5)启动(重启)验证
启动:${nginx}/sbin/nginx
重启:${nginx}/sbin/nginx -s reload
${nginx} 安装路径, /usr/local/nginx
(6)访问验证
使用默认80端口访问验证:http://localhost:80 或http://127.0.0.1:80 ,本次验证,我们使用的虚拟机因此http://172.31.8.112:80
7. Nginx 虚拟域名配置
8. Nginx 本地搭建环境注意事项
(1). 可以配置域名转发,但是请注意要配置host,并且使host生效之后才可以,设置完成之后要重启浏览器
(2). Linux
sudo vi /etc/hosts
添加好对应的域名及IP