Skip to main content

【nginx 配置】

一、prce 的安装

1、下载:

wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz

2、解压:

tar zxvf pcre-8.35.tar.gz

3、安装

cd pcre-8.35

./configure

make && make install

二、Nginx 的安装

1、下载

wget http://nginx.org/download/nginx-1.6.2.tar.gz

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

}

四、重启

1、杀掉 nginx 进程

killall -9 nginx

2、重新加载配置文件

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

3、检查配置是否正确

nginx -t

4、重新启动 nginx

进入到 nginx 文件夹下

cd /usr/local/nginx/sbin/

检测 nginx 配置

./nginx -t

重启 nginx

./nginx -s reload

五、遇到的问题

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