nginx安装与使用

98 阅读1分钟

【nginx配置】

一、prce的安装

1、下载:

wget downloads.sourceforge.net/project/pcr…

2、解压:

tar zxvf pcre-8.35.tar.gz

3、安装

cd pcre-8.35

./configure

make && make install

二、Nginx的安装

1、下载

wget nginx.org/download/ng…

2、安装

./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35

make

make install

三、配置二级域名

利用二级域名是一种充分利用的域名资源的方法,同样利用路径也可以,这和使用的服务器内部采用的映射方式有关,比如院网和工作室网站对外表现就是不同的网站,但是工作室网站的/hope只是一个路径而已,Nginx不能根据路径,可以使用二级域名使得不同应用运行在同一个一级域名下。 以下的Nginx配置,打开不同域名也就访问了不同网站:


#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   /home/web/library;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }

        #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;
        #}
    }

server {
        listen       80;
        #域名
        server_name  haisheteam.com code1.haisheteam.com;

        location / {
             #node.js应用的端口
             proxy_pass http://127.0.0.1:3000;
             root blog;
       }
        #静态文件交给Nginx直接处理
        #location ~ *^.+\.(css | js | txt | swf | mp4)$ {
         #   root E:\huruji\blog\wechat_v1.1\public;
          #   access_log off;
           # expires 24h;
       #}
    }


    # 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;
    #    }
    #}

}

四、nginx命令

# windows启动
> start nginx

# linux/mac启动
$ service nginx start

# 手动指定配置
$ nginx -c /usr/local/nginx/conf/nginx.conf

# -p指定nginx运行目录(日志存储位置)
$ nginx -c /path/nginx.conf -p /path/

# 重启
$ nginx -s reload

# 关闭
$ nginx -s stop

# 查看端口
$ netstat -an | grep 端口  # linux/mac系统
> netstat -an | findstr 端口  # windows系统

# 测试web服务
$ curl -i 主机:端口
# 
$ telnet 主机 端口

# 查看进程
$ ps -ef | grep nginx

# 查看错误日志
$ tail -30 /var/log/nginx/error.log

五、遇到的问题

1、vue项目页面刷新导致404

Nginx的配置语法灵活,可控制度非常高。在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率。

修改nginx配置文件

location / {
root ...
index ...
try_files $uri $uri/ /index.html; ---解决页面刷新404问题
} 

查看nginx是否启动

ps -ef | grep nginx