相当于Nginx是一个服务器,不过只能部署静态的资源

1、选择一个静态资源的模板项目

即:不包含jsp、servlet等的静态资源文件,如html、js、img等

2、选择一个地址

这里选择:/opt/myweb

  1. 目录结构
  2. /opt
  3. --
  4. --/myweb
  5. --
  6. --index.html

3、配置nginx中的location地址

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. worker_connections 1024;
  9. }
  10. http {
  11. include mime.types;
  12. default_type application/octet-stream;
  13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  14. # '$status $body_bytes_sent "$http_referer" '
  15. # '"$http_user_agent" "$http_x_forwarded_for"';
  16. #access_log logs/access.log main;
  17. sendfile on;
  18. #tcp_nopush on;
  19. #keepalive_timeout 0;
  20. keepalive_timeout 65;
  21. #gzip on;
  22. server {
  23. listen 80;
  24. server_name localhost;
  25. #charset koi8-r;
  26. #access_log logs/host.access.log main;
  27. #在这里配置----------------------------------------------------------------------------
  28. location / {
  29. #配置根目录地址,注意上面的location / == root /opt/myweb;,前者表示的是拦截的地址,后置表示的是拦截的地址指示的根目录
  30. root /opt/myweb;
  31. #索引页面
  32. index index.html;
  33. }
  34. #在这里配置----------------------------------------------------------------------------
  35. #error_page 404 /404.html;
  36. # redirect server error pages to the static page /50x.html
  37. #
  38. error_page 500 502 503 504 /50x.html;
  39. location = /50x.html {
  40. root html;
  41. }
  42. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  43. #
  44. #location ~ \.php$ {
  45. # proxy_pass http://127.0.0.1;
  46. #}
  47. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  48. #
  49. #location ~ \.php$ {
  50. # root html;
  51. # fastcgi_pass 127.0.0.1:9000;
  52. # fastcgi_index index.php;
  53. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  54. # include fastcgi_params;
  55. #}
  56. # deny access to .htaccess files, if Apache's document root
  57. # concurs with nginx's one
  58. #
  59. #location ~ /\.ht {
  60. # deny all;
  61. #}
  62. }
  63. # another virtual host using mix of IP-, name-, and port-based configuration
  64. #
  65. #server {
  66. # listen 8000;
  67. # listen somename:8080;
  68. # server_name somename alias another.alias;
  69. # location / {
  70. # root html;
  71. # index index.html index.htm;
  72. # }
  73. #}
  74. # HTTPS server
  75. #
  76. #server {
  77. # listen 443 ssl;
  78. # server_name localhost;
  79. # ssl_certificate cert.pem;
  80. # ssl_certificate_key cert.key;
  81. # ssl_session_cache shared:SSL:1m;
  82. # ssl_session_timeout 5m;
  83. # ssl_ciphers HIGH:!aNULL:!MD5;
  84. # ssl_prefer_server_ciphers on;
  85. # location / {
  86. # root html;
  87. # index index.html index.htm;
  88. # }
  89. #}
  90. }

4、启动nginx

4.1、配置环境变量

  1. #在 /etc/profile中编辑
  2. vi /etc/profile
  3. NGINX_HOME=/nginx地址
  4. PATH=${NGINX_HOME}/sbin:$PATH
  5. export PATH

4.2、启动命令

  1. #配置了环境变量可以在任何地方直接使用 nginx启动服务,否则使用sbin目录下的nginx文件启动
  2. #直接启动,使用默认配置文件ngnx.conf.defualt
  3. nginx
  4. #指定配置文件启动
  5. nginx -c nginx.conf
  6. #检查配置文件的语法是否正确
  7. nginx -c nginx.conf -t

4.2、重启命令

  1. nginx -s reload

4.3、停止命令

  1. #强制停止
  2. nginx -s stop
  3. #不再接收请求,已经请求的请求完成之后退出
  4. nginx -s quit

4.4、浏览器地址

直接输入ip地址,不需要端口