Nginx基础教程

综合参考

https://www.linode.com/docs/web-servers/nginx/how-to-configure-nginx
http://www.jianshu.com/p/9a6c96ecc8b8

 极速教程

安装

CentOS 64
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx
//启动停止
whereis nginx
找到位置
service nginx start
/usr/sbin/nginx -s stop//reload
/usr/sbin/nginx -t //测试配置文件
service nginx status//当前状态

开机自启动chkconfig nginx on
添加端口号:
永久性添加只需要下面2条命令之一:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --permanent --add-service=http
重新载入,即可
firewall-cmd --reload

对应的查询和删除命令:
firewall-cmd --query-port=80/tcp
查看哪些端口打开了firewall-cmd --zone=public --list-ports
查看哪些服务打开了:firewall-cmd --list-services ,查看哪些服务可以打开firewall-cmd --get-services

firewall-cmd --zone=public --remove-port=80/tcp --permanent
firewall-cmd --remove-service=http --zone=public --permanent

service firewalld //start stop status enable disable restart

 

原文链接

1.静态服务器

server {
    listen 80; # 端口号
    location / {
        root /usr/share/nginx/html; # 静态文件路径
    }
}

 

 2.反向代理

 

server {
    listen 80;
    location / {
        proxy_pass http://192.168.20.1:8080; # 应用服务器HTTP地址
    }
}

 

3.负载均衡

upstream myapp {
    server 192.168.20.1:8080; # 应用服务器1
    server 192.168.20.2:8080; # 应用服务器2
}
server {
    listen 80;
    location / {
        proxy_pass http://myapp;
    }
}

 

4.虚拟主机

server {
    listen 80 default_server;
    server_name _;
    return 444; # 过滤其他域名的请求,返回444状态码
}
server {
    listen 80;
    server_name www.aaa.com; # www.aaa.com域名
    location / {
        proxy_pass http://localhost:8080; # 对应端口号8080
    }
}
server {
    listen 80;
    server_name www.bbb.com; # www.bbb.com域名
    location / {
        proxy_pass http://localhost:8081; # 对应端口号8081
    }
}

 

5.FastCGI

server {
    listen 80;
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /PHP文件路径$fastcgi_script_name; # PHP文件路径
        fastcgi_pass 127.0.0.1:9000; # PHP-FPM地址和端口号
        # 另一种方式:fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

 

详细教程

原文链接

例子1:静态标准服务器

 

server {
        listen       80;
        server_name  localhost;
    

        location / {
            root myhtml;
            index  index.html index.htm;
        autoindex on;
        }
    
}

 nginx\myhtml

....................\.index.html

.....................\blog

...........................\otherfiles

.............................\somepath

............................................\circuit.png

............................................\hi.html

以上是windows下的文件目录图。

访问localhost,会以index.html的内容显示主页。

因为打开了autoindex,访问 localhost/blog,会显示

 

 

 例子2:特殊文件结尾的处理

server {
        listen       80;
        server_name  localhost;
    

        location / {
            root myhtml;
            index  index.html index.htm;
        autoindex on;
        }
    location ~ \.png{
    root images;
    }
    
}

nginx/images

..................pic/circuit.png

..................circuit.png

访问localhost/circuit.png

localhost/pic/circuit.png

都能显示该图片。

 例子3:处理location /somepath这样的URI

server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root myhtml;
            index  index.html index.htm;
        
        }
    
    
    location /blog/ {
    root myhtml2;
    #index hi.html;
    autoindex on;
}

myhtml2结构图:

 nginx\myhtml2

....................\.index.html

.....................\blog

...........................\hi.html

.............................\somepath

............................................\circuit.png

............................................\hi.html

访问localhost/blog,

localhost/blog/somepath都显示目录图。访问类似localhost/blog/somepath/circuit.png显示具体图片。

如果index hi.html打开

目录下有hi.html的话,显示对应html,没有则会103 Forbidden。

 

posted on 2017-07-11 08:38  legion  阅读(363)  评论(0编辑  收藏  举报

导航