nginx在Centos和Mac的安装和使用
1. Centos安装nginx
1.1 使用yum安装
yum install nginx -y
1.2 开放端口
nginx默认监听80端口
#见nginx配置文件/etc/nginx/nginx.conf
server {
listen 80;
server_name localhost;
...
所以还需在iptables中开放80端口。如果更改成别的端口,则也需去iptables中开放此端口。
# vim /etc/sysconfig/iptables
在-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
后面增加一行
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
保存后退出
- 重启Centos的防火墙
service iptables restart
1.3 nginx使用
- 启动nginx服务器:
service nginx start
- 重启nginx服务器:
service nginx restart
- 停止nginx服务器:
service nginx stop
- 查看nginx服务器状态:
service nginx status
2. Mac安装nginx
2.1 使用brew安装
brew install nginx
2.2 nginx使用
- 启动nginx服务器:
nginx
- 重启nginx服务器:
nginx -s reload
- 立即停止nginx服务器:
nginx -s stop
- 完整而有序地停止nginx服务器:
nginx -s quit
3. nginx配置文件解读
一般不要修改以.default结尾的默认配置文件,这些默认配置文件用于方便我们将配置过的.conf文件恢复到初始状态。
Centos的nginx配置文件默认在/etc/nginx/nginx.conf
Mac的nginx配置文件默认在/usr/local/etc/nginx/nginx.conf
Nginx配置文件主要分成四部分:main(全局设置)、server(主机设置)、upstream(上游服务器设置,主要为反向代理、负载均衡相关配置)和 location(URL匹配特定位置后的设置),每部分包含若干个指令。
-
main部分设置的指令将影响其它所有部分的设置;
-
server部分的指令主要用于指定虚拟主机域名、IP和端口;
-
upstream的指令用于设置一系列的后端服务器,设置反向代理及后端服务器的负载均衡;
-
location部分用于匹配网页位置(比如,根目录“/”,“/images”,等等)。他们之间的关系式:server继承main,location继承server;upstream既不会继承指令也不会被继承。它有自己的特殊指令,不需要在其他地方的应用。
#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 8080;
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;
#}
}
# 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;
# }
#}
# 将servers文件夹下的配置文件都加载进来,和直接写在这个文件中的效果相同,但更容易维护些
include servers/*;
}