linux nginx安装 & 简单配置

Linux安装nginx

1. 安装pcre

下载地址:https://sourceforge.net/projects/pcre/files/pcre/8.38/pcre-8.38.zip/download

解压,config,编译,安装

    ./configure --prefix=/home/yourpath/pcre
    make && make install

2. 安装openssl

下载地址: https://www.openssl.org/source/openssl-1.0.2g.tar.gz

解压,config,编译,安装

    ./config --prefix=/home/yourpath/openssl
    make && make install

3. 安装zlib

下载地址: http://zlib.net/zlib-1.2.8.tar.gz

解压,config,编译,安装

    ./configure --prefix=/home/yourpath/zlib
    make && make install

4. 安装nginx

下载地址: http://nginx.org/download/nginx-1.9.12.tar.gz

解压,config,编译,安装

    ./configure --prefix=/home/yourpath/nginx --with-pcre=/home/yourpath/pcre-8.38 --with-openssl=/home/yourpath/openssl-1.0.2g --with-zlib=/home/yourpath/zlib-1.2.8
    make && make install

顺利的话,nginx安装完毕,安装路径为/home/yourpath/nginx

5. 测试

    cd /home/yourpath/nginx/sbin
    ./nginx  //启动nginx

用浏览器访问即可,默认80端口。如果出现下面的提示,则表示安装成功.

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

    For online documentation and support please refer to nginx.org.
    Commercial support is available at nginx.com.

    Thank you for using nginx.

6. 配置

主要的配置在/home/yourpath/nginx/conf/nginx.conf文件中

    worker_processes  1; //配置多少个work进程
    worker_connections  1024; //每个工作进程最大的连接数
    listen       80; //默认监听80端口

一般情况下,一个nginx下可能配置多个域名,会增加多个配置文件,在nginx.conf默认添加 include vhosts/*.conf;,然后将自己增加的配置文件都放在vhosts目录下.

静态html文件代理

vhosts/static.yeetrack.conf 内容如下:

    server {
        listen 80;
        server_name nginx.yeetrack.com;
        access_log logs/yeetrack.access.log;
        error_log logs/yeetrack.error.log;

        location / {
            root /home/victor/work/nginx/myhtml;
        }
    }

将静态的html、css、js文件放在/home/victor/work/nginx/myhtml路径下即可。
reload nginx:nginx/sbin/nginx -s reload

tomcat服务代理

nginx代理tomcat等服务,vhosts/tomcat.conf内容如下:

    upstream tomcat {
        server 172.17.80.51:8080;
    }

    server {
        listen 80;
        server_name nginx.yeetrack.com;
        access_log logs/yeetrack.access.log;
        error_log logs/yeetrack.error.log;

        location / {
            proxy_pass http://tomcat;
        }
    }

reload nginx: nginx/sbin/nginx -s reload

php服务代理

nginx代理php服务,vhosts/php.conf内容如下:
遇到*.php结尾的请求,nginx会将请求转发到127.0.0.1的9000端口,然后php会在本机的/home/victor/work/static目录中寻找响应的php文件进行解析;其他的请求nginx会当做静态资源,在nginx机器的/home/victor/work/static目录中查找相应文件。

    upstream php {
        server 127.0.0.1:9000;
    }

    server {
        listen 80;
        server_name php.yeetrack.com;
        access_log logs/yeetrack.access.log main;
        error_log logs/yeetrack.error.log;
        root /home/victor/work/static;


        location ~ .php$ {
            fastcgi_pass   php;
            fastcgi_next_upstream error timeout invalid_header http_500;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi.conf;
        }
    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注

(Spamcheck Enabled)